1 $arr1 = array(1, 2, 3, 4, ‘color‘=>‘red‘); 2 $arr2 = array(‘a‘, ‘b‘, ‘c‘, ‘color‘=>‘blue‘); 3 print_r(array_merge($arr1, $arr2));//同名索引的值会覆盖 4 print_r(array_merge_recursive($arr1, $arr2));//相同的键名 不会覆盖,如果是单个元素会在转为一个一维数组
两个函数作用都是合并数组。参数可以是1到n个数组。(呃,不明白参数是1个数组的时候啥用的。谁知道告诉我。)
输出结果:
第3行:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[color] => blue //这一行有区别注意哦
[4] => a
[5] => b
[6] => c
)
第4行:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[color] => Array //注意这个地方有区别
(
[0] => red
[1] => blue
)
[4] => a
[5] => b
[6] => c
)
时间: 2024-10-11 23:35:29