Last active
April 24, 2018 07:35
-
-
Save underhilllabs/7206369 to your computer and use it in GitHub Desktop.
Important Lesson on php's array union (+) operator and array_merge() function
This file contains 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 | |
$a[0] = 1; | |
$a[1] = 2; | |
$a[2] = 3; | |
$b[0] = 'a'; | |
$b[1] = 'b'; | |
$b[2] = 'c'; | |
$b[3] = 1; | |
print "a array: \n"; | |
print_r($a); | |
print "b array: \n"; | |
print_r($b); | |
// array union op, unions on key! not val!! | |
$plus = $a + $b; | |
print "array + \n"; | |
print_r($plus); | |
$arr_merge = array_merge($a, $b); | |
print " array w merge\n"; | |
print_r($arr_merge); | |
$arr_merge_uniq = array_unique(array_merge($a, $b)); | |
print " array w merge and uniq\n"; | |
print_r($arr_merge_uniq); |
This file contains 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
a array: | |
Array | |
( | |
[0] => 1 | |
[1] => 2 | |
[2] => 3 | |
) | |
b array: | |
Array | |
( | |
[0] => a | |
[1] => b | |
[2] => c | |
[3] => 1 | |
) | |
array + | |
Array | |
( | |
[0] => 1 | |
[1] => 2 | |
[2] => 3 | |
[3] => 1 | |
) | |
array w merge | |
Array | |
( | |
[0] => 1 | |
[1] => 2 | |
[2] => 3 | |
[3] => a | |
[4] => b | |
[5] => c | |
[6] => 1 | |
) | |
array w merge and uniq | |
Array | |
( | |
[0] => 1 | |
[1] => 2 | |
[2] => 3 | |
[3] => a | |
[4] => b | |
[5] => c | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello, with what version of PHP supports array + array ?