Created
June 25, 2024 09:56
-
-
Save alpenzoo/b5fb71ea5357c8aef650740cedc088ce to your computer and use it in GitHub Desktop.
PHP merge array variations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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