Skip to content

Instantly share code, notes, and snippets.

@Maikuolan
Created February 11, 2016 08:39
Show Gist options
  • Save Maikuolan/12ec987b6237f7692147 to your computer and use it in GitHub Desktop.
Save Maikuolan/12ec987b6237f7692147 to your computer and use it in GitHub Desktop.
Differences in how array_merge and how "+" handles conflicting indexes.
<?php
$arr1 = array(
'a' => 'Hello',
'b' => 'World',
'c' => 'Foo',
'd' => 'Bar'
);
$arr2 = array(
'a' => 'Fello',
'b' => 'Borld',
'c' => 'Hoo',
'd' => 'War'
);
$arr3 = array(
0 => 1,
1 => 2,
2 => 3,
3 => 4
);
$arr4 = array(
0 => 5,
1 => 6,
2 => 7,
3 => 8
);
echo "1: ";
var_dump(array_merge($arr1, $arr2, $arr3, $arr4));
echo "\n\n";
echo "2: ";
var_dump($arr1 + $arr2 + $arr3 + $arr4);
echo "\n\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment