Skip to content

Instantly share code, notes, and snippets.

@alpenzoo
Created June 25, 2024 09:56
Show Gist options
  • Save alpenzoo/b5fb71ea5357c8aef650740cedc088ce to your computer and use it in GitHub Desktop.
Save alpenzoo/b5fb71ea5357c8aef650740cedc088ce to your computer and use it in GitHub Desktop.
PHP merge array variations
<?php
// Existing array
$arr = array(1, 2, 3);
// New element to be added at 'zero' => 0
// Create an array using the new element
$temp = array('one');
$temp2 = array("hhhhhh");
// Append the $temp in the beginning of $arr
// Using array union(+) operator
echo "Result of array union(+) : ";
print_r([...$temp, ...$arr, ...$temp2]);
echo "Result of array union(+) : ";
$arr2 = $temp + $arr + $temp2;
print_r($arr2);
echo "Result merge";
print_r(array_merge($arr, $temp));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment