Last active
May 27, 2016 20:10
-
-
Save FMCorz/c2c89d5ed35cd2321f5db4cb175ef575 to your computer and use it in GitHub Desktop.
PHP array merging
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 | |
// Non-associative. | |
$a = [1, 2, 3]; | |
$b = [4, 5, 6, 7]; | |
$c = $a + $b; | |
var_dump($c); | |
$d = array_merge($a, $b); | |
var_dump($d); | |
$a += $b; | |
var_dump($a); | |
echo '----' . PHP_EOL; | |
// Associative. | |
$a = ['one' => 1, 'two' => 2, 'three' => 3]; | |
$b = ['one' => 4, 'two' => 5, 'three' => 6, 'four' => 7]; | |
$c = $a + $b; | |
var_dump($c); | |
$d = array_merge($a, $b); | |
var_dump($d); | |
$a += $b; | |
var_dump($a); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Plus, you have to be careful about using string keys that "look" like integers.
array(4) { [34987]=> int(1) [48907]=> int(2) [12039]=> int(3) [89074]=> int(7) }
array(7) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> int(6) [6]=> int(7) }