我不太清楚如何做到这一点。假设我有2个关联数组
$arr1 = array('a' => "1", 'b' => "2", 'c' => "3");
$arr2 = array('a' => "9", 'b' => "8", 'c' => "7");
我怎么可能产生一个“加总”数组,如下所示
$arr1 = array(
array('a', "1", "9"),
array('b', "2", "8"),
array('c', "3", "7")
);
我不确定上面的语法是否正确。如果不是,那么看起来像下面的加起来也会这样做
$arr1 = array(
'a' => array("1", "9"),
'b' => array("2", "8"),
'c' => array("3", "7")
);
由于
答案 0 :(得分:3)
foreach($arr1 as $k=>$v) {
$new[$k] = array($v, $arr2[$k]);
}
我认为你想要什么。但如果我弄错了,你可以这样做:
foreach($arr1 as $k=>$v) {
$new[] = array($k, $v, $arr2[$k]);
}
答案 1 :(得分:0)
$arr1 = array('a' => "1", 'b' => "2", 'c' => "3");
$arr2 = array('a' => "9", 'b' => "8", 'c' => "7");
$summ=array();
foreach(array($arr1,$arr2) as $arr){
$keys=array_keys($arr);
foreach($keys as $key){
if(isset($summ[$key]))
$summ[$key][]=$arr[$key];
else $summ[$key]=array($arr[$key];
}
}
/*
This will have made:
$sum = array(
'a' => array("1", "9"),
'b' => array("2", "8"),
'c' => array("3", "7")
);
I leave it up to you to now reduce this one more step to match your desired output.
*/