There are three ways to merge php Arrays: array_merge, array_merge_recursive, + (the plus sign of the mathematical operator). The following is a comparison code $ array1array (2, 4, colorred); $ array2array (a, B, colorgreen, shapetrapezoid, 4); $ resultarray_merge ($ array
There are three ways to merge php Arrays: array_merge, array_merge_recursive, + (the plus sign of the mathematical operator). The following is a comparison code $ array1 = array (2, 4, "color" = "red"); $ array2 = array ("a", "B", "color" = "green", "shape" = "trapezoid ", 4); $ result = array_merge ($ array
There are three ways to merge php Arrays: array_merge, array_merge_recursive, + (that is, the plus sign of the mathematical operator );
The following is a comparison code.
$array1 = array(2,4,"color" => "red"); $array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4); $result = array_merge($array1, $array2); echo "----------------array_merge---------------".PHP_EOL; print_r($result); echo "----------------+++++++++++---------------".PHP_EOL; print_r($array1+$array2); echo "----------------array_merge_recursive---------------".PHP_EOL; print_r(array_merge_recursive($array1,$array2));
The result is as follows:
----------------array_merge---------------Array( [0] => 2 [1] => 4 [color] => green [2] => a [3] => b [shape] => trapezoid [4] => 4)----------------+++++++++++---------------Array( [0] => 2 [1] => 4 [color] => red [shape] => trapezoid [2] => 4)----------------array_merge_recursive---------------Array( [0] => 2 [1] => 4 [color] => Array ( [0] => red [1] => green ) [2] => a [3] => b [shape] => trapezoid [4] => 4)
Compare the "color" values of array_merge and + and the results of array_merge_recursive, we can see that:
1. For the same string index,
Array_merge uses the following value to overwrite the preceding value;
+ The same key is overwritten with the previous value;
Array_merge_recursive places the same index in an array to add the array dimension;
2. For the same numeric index,
Array_merge will re-index the repeated values (Index value starts from 0);
+ The subsequent values are still overwritten with the previous values;
Array_merge_recursive is the same as array_merge.
Original article address: Comparison of php one-dimensional array merging methods, thanks to the original author for sharing.