我的数组具有相同的键,如下所示:
Array1(
[00] => green
[10] => red
[20] => yellow
)
Array2(
[00] => avocado
[10] => apple
[20] => banana
)
我想要这个:
Array_result(
[00] => Array(
[0] => green
[1] => avocado
)
[10] => Array(
[0] => red
[1] => apple
)
[20] => Array(
[0] => yellow
[1] => banana
))
或[0],[1],[2],我不介意密钥,我尝试使用array_merge,但它不能正常工作。
编辑:我不知道为什么,但是array_merge_recursive会打印出来:Array
(
[00] => Array
(
[0] => green
[1] => avocado
)
[0] => red
[1] => apple
[2] => yellow
[3] => banana
)
答案 0 :(得分:4)
您正在寻找array_merge_recursive
而不是array_merge
。
<?php
$A = Array(00 => "green", 10 => "red", 20 => "yellow");
$B = Array(00 => "avocado", 10 => "apple", 20 => "banana");
$result = array_merge_recursive($A, $B);
print_r($result);?>
答案 1 :(得分:0)
试试:
function merge($arr){
$n = array();
if(!empty($arr)){
foreach($arr as $a){
if(!empty($a)){
foreach($a as $k => $v){
$n[$k][] = $v;
}}}}
return $n;
}
// use
$a = array('00' => 'green', '10' => 'red', '20' => 'yellow');
$b = array('00' => 'avocado', '10' => 'apple', '20' => 'apple');
print_r(merge(array($a, $b)));