php数组合并有三种方式:array_merge,array_merge_recursive,+(就是数学运算符加号); 下面是一段对比的代码 $array1 = array(2,4,"color" = "red"); $array2 = array("a", "b", "color" = "green", "shape" = "trapezoid", 4); $result = array_merge($array
php数组合并有三种方式:array_merge,array_merge_recursive,+(就是数学运算符加号);
下面是一段对比的代码
$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));
结果如下所示
----------------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 )
对比array_merge和+以及array_merge_recursive结果的”color”的值我们可以看出:
1.对于相同的字符串索引,
array_merge则会用后面的值覆盖前面出现的值;
+会用前面出现过的值覆盖后面相同的key;
array_merge_recursive则会把相同的索引放到一个数组里面,增加数组的维度;
2.对于相同的数字索引,
array_merge则会给重复的值重建索引(索引值从0开始);
+仍然是用前面出现过的值覆盖后面的值;
array_merge_recursive和array_merge的处理方法一样。
原文地址:php一维数组合并的三种方式对比, 感谢原作者分享。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool