解析php中array_merge与array+array的区别_PHP教程
程序员文章站
2022-03-25 21:21:45
...
array_merge是丢弃原来的数字的key,而保留字符串形式的key,然后组成一个新的数组,不管键名是否一样,都不合并,除非键名和value同时一样并且还必须是字符串形式的key才合并。而array+array就是不管你是什么情况,它都只会先把前面的数组的数据先放到新生成的数组中,之后再看第二个array是否比第一个数组个数多,多的就添加进来,它只数个数,但是这种情况它会添加进来:$a = array('d'=>'aass','e'=>'adsdfd','asd'=>'asdsdd','ddfg'=>'dssdf');
$b = array('d'=>'adddd','adsdfd','asdfsddddd','d'=>'aass');
$d = $a + $b;
$e = array_merge($a,$b);
var_dump($d);
var_dump($e);
打印:
array
'd' => string 'aass' (length=4)
'e' => string 'adsdfd' (length=6)
'asd' => string 'asdsdd' (length=6)
'ddfg' => string 'dssdf' (length=5)
0 => string 'adsdfd' (length=6)
1 => string 'asdfsddddd' (length=10)
array
'd' => string 'aass' (length=4)
'e' => string 'adsdfd' (length=6)
'asd' => string 'asdsdd' (length=6)
'ddfg' => string 'dssdf' (length=5)
0 => string 'adsdfd' (length=6)
1 => string 'asdfsddddd' (length=10)
复制代码 代码如下:
$b = array('d'=>'adddd','adsdfd','asdfsddddd','d'=>'aass');
$d = $a + $b;
$e = array_merge($a,$b);
var_dump($d);
var_dump($e);
打印:
array
'd' => string 'aass' (length=4)
'e' => string 'adsdfd' (length=6)
'asd' => string 'asdsdd' (length=6)
'ddfg' => string 'dssdf' (length=5)
0 => string 'adsdfd' (length=6)
1 => string 'asdfsddddd' (length=10)
array
'd' => string 'aass' (length=4)
'e' => string 'adsdfd' (length=6)
'asd' => string 'asdsdd' (length=6)
'ddfg' => string 'dssdf' (length=5)
0 => string 'adsdfd' (length=6)
1 => string 'asdfsddddd' (length=10)
推荐阅读
-
解析php中获取url与物理路径的总结
-
解析php中array_merge与array+array的区别
-
解析php函数method_exists()与is_callable()的区别
-
PHP API中,MYSQL与MYSQLI的持久连接区别
-
PHP中mb_convert_encoding与iconv函数的深入解析
-
解析php中var_dump,var_export,print_r三个函数的区别
-
PHP中isset与array_key_exists的区别实例分析
-
PHP中array_merge和array相加的区别分析
-
深入分析php中接口与抽象类的区别
-
php中$_POST与php://input的区别实例分析