The two methods of merging arrays in PHP and the differences are introduced, the need for friends can refer to the following
Two methods and differences of PHP array merging
If it is an associative array, the following:
Copy the Code code as follows:
$a = Array (
' WHERE ' = ' uid=1 ',
' Order ' = ' uid ',
);
$b = Array (
' WHERE ' = ' uid=2 ',
' Order ' = ' uid desc ',
);
1. Array_merge, if two arrays have the same key, the latter one will overwrite the previous
Copy the Code code as follows:
<?php
$c = Array_merge ($a, $b);
Var_export ($c);//The result is the same as the original $b
$d = Array_merge ($b, $a);
Var_export ($d);//The result is the same as the original $ A
2. The "+" operator, if the two array has the same key, the previous one will overwrite the following
Copy the Code code as follows:
<?php
$c = $a + $b;
Var_export ($c);//The result is the same as the original $ A
$d = $b + $a;
Var_export ($d);//The result is the same as the original $b
If it is a numeric index array, as follows:
Copy the Code code as follows:
$a = Array (
1 = ' 1111111 ',
2 = ' 222222222 '
);
$b = Array (
4 = ' 33333333333 ',
1 = ' 444444444 '
);
1. Array_merge. The effect resembles code foreach each array element and then presses each element into a new stack
Copy the Code code as follows:
<?php
$c = Array_merge ($a, $b);
Var_export ($c);
$d = Array_merge ($b, $a);
Var_export ($d);
Output:
Array (
0 = ' 1111111 ',
1 = ' 222222222 ',
2 = ' 33333333333 ',
3 = ' 444444444 ',
)
Array (
0 = ' 33333333333 ',
1 = ' 444444444 ',
2 = ' 1111111 ',
3 = ' 222222222 ',
)
2. The "+" operator. The effect is similar to the code foreach each array element, then presses each element into a new stack, and if the same key already exists, does not process
Copy the Code code as follows:
<?php
$c = $a + $b;
Var_export ($c);
$d = $b + $a;
Var_export ($d);
Output:
Array (
1 = ' 1111111 ',
2 = ' 222222222 ',
4 = ' 33333333333 ',
)
Array (
4 = ' 33333333333 ',
1 = ' 444444444 ',
2 = ' 222222222 ',
)