PHP内置函数合并数组常用的有这几种方式
$arr = ['a1','a2','a3'];
$arr1 = ['a3','a4','a5'];
$arr_ = array_merge($arr,$arr1);//Array ( [0] => a1 [1] => a2 [2] => a3 [3] => a3 [4] => a4 [5] => a5 )索引数组没有相同key进行合并数组 而是数组相加
那如果不是单纯的索引数组呢
$arr2 = ['a1'=>1,'a2'=>2,5,7];
$arr3 = ['a1'=>1,'a3'=>3,5];
$arr_1 = array_merge($arr2,$arr3);//Array ( [a1] => 1 [a2] => 2 [0] => 5 [1] => 7 [a3] => 3 [2] => 5 )
// 结论 array_merge 他会合并相同key 的2个数组 数字索引的,则键名会以连续方式重新索引。
print_r($arr_1);
我们在来测一下数组+数组
$arr2 = ['a1'=>1,'a2'=>2,5,7];
$arr3 = ['a1'=>1,'a3'=>3,6];
$arr_2 = $arr2+$arr3;
print_r($arr_2);//Array ( [a1] => 1 [a2] => 2 [0] => 6 [1] => 7 [a3] => 3 )他会摒弃第一个数组相同key,如 第一个数组中0=》5换成了第二个0=》6 a3=》3 a2=》2 因为key第二个数组里不存在他就保留下来
还有一个是array_merge_service
$arr4 = ['ceshi'=>['uid'=>1,'bq'=>4],'s'=>85,87];
$arr5 = ['ceshi'=>['uid'=>1,'bq'=>4],'d'=>85,6];
$arr3_ = array_merge_recursive($arr4,$arr5);
print_r($arr3_);//Array ( [ceshi] => Array ( [uid] => Array ( [0] => 1 [1] => 1 ) [bq] => Array ( [0] => 4 [1] => 4 ) ) [s] => 85 [0] => 87 [d] => 85 [1] => 6 )
//他会递归进行合并 不管多少维数组 他都会把同一key下的值放在一起形成对应的键值数组重新组成多维数组