我需要合并一个多维数组,以便合并后的新值将等于0而不是原始值。
$data = array(
0 => array(
'2009-05-12' => string '1' (length=1)
'2010-12-07' => string '8' (length=1)
'2010-12-02' => string '23' (length=2)
'2010-11-11' => string '16' (length=2)
'2010-09-01' => string '1' (length=1)
'2010-08-24' => string '1' (length=1)
'2010-06-30' => string '1' (length=1)
), 1 => array(
'2010-09-08' => string '1' (length=1)
'2011-11-28' => string '1' (length=1)
'2011-09-28' => string '3' (length=1)
'2011-09-21' => string '1' (length=1)
'2012-04-10' => string '21' (length=2)
'2012-04-02' => string '12' (length=2)
), 2 => array(
'2010-12-15' => string '12' (length=2)
'2010-12-08' => string '12' (length=2)
'2010-11-28' => string '12' (length=2)
'2010-06-30' => string '12' (length=2)
'2010-05-04' => string '1' (length=1)
'2011-12-21' => string '9' (length=1)
'2012-01-05' => string '23' (length=2)
), 3 => array(
'2010-12-14' => string '2' (length=1)
'2010-12-10' => string '4' (length=1)
'2010-11-29' => string '4' (length=1)
'2011-10-04' => string '7' (length=1)
'2011-09-27' => string '11' (length=2)
'2012-01-06' => string '1' (length=1)
)
);
我成功的做法是创建一个合并数组中元素的循环:
foreach($data as $index => $date){
foreach($data as $index2 => $date2)
$data[$index] = array_merge($data[$index2], $data[$index]);
}
上述脚本几乎输出正确的结果,但合并的值应该等于0而不是原始值。
上述脚本的输出合并了每个嵌套数组,以便它包含所有剩余嵌套数组的值。
输出应为:
$data = array(
0 => array(
'2009-05-12' => string '1' (length=1)
'2010-12-07' => string '8' (length=1)
'2010-12-02' => string '23' (length=2)
'2010-11-11' => string '16' (length=2)
'2010-09-01' => string '1' (length=1)
'2010-08-24' => string '1' (length=1)
'2010-06-30' => string '1' (length=1)
'2010-09-08' => string '0' (length=1)
'2011-11-28' => string '0' (length=1)
'2011-09-28' => string '0' (length=1)
'2011-09-21' => string '0' (length=1)
'2012-04-10' => string '0' (length=1)
'2012-04-02' => string '0' (length=1)
'2010-12-15' => string '0' (length=1)
'2010-12-08' => string '0' (length=1)
'2010-06-30' => string '0' (length=1)
'2010-05-04' => string '0' (length=1)
'2011-12-21' => string '0' (length=1)
'2012-01-05' => string '0' (length=1)
'2010-12-14' => string '0' (length=1)
'2010-12-10' => string '0' (length=1)
'2010-11-29' => string '0' (length=1)
'2011-10-04' => string '0' (length=1)
'2011-09-27' => string '0' (length=1)
'2012-01-06' => string '0' (length=1)
), 1 => array(
'2009-05-12' => string '0' (length=1)
'2010-12-07' => string '0' (length=1)
'2010-12-02' => string '0' (length=1)
'2010-11-11' => string '0' (length=1)
'2010-09-01' => string '0' (length=1)
'2010-08-24' => string '0' (length=1)
'2010-06-30' => string '0' (length=1)
'2010-09-08' => string '1' (length=1)
'2011-11-28' => string '1' (length=1)
'2011-09-28' => string '3' (length=1)
'2011-09-21' => string '1' (length=1)
'2012-04-10' => string '21' (length=2)
'2012-04-02' => string '12' (length=2)
'2010-12-15' => string '0' (length=1)
'2010-12-08' => string '0' (length=1)
'2010-06-30' => string '0' (length=1)
'2010-05-04' => string '0' (length=1)
'2011-12-21' => string '0' (length=1)
'2012-01-05' => string '0' (length=1)
'2010-12-14' => string '0' (length=1)
'2010-12-10' => string '0' (length=1)
'2010-11-29' => string '0' (length=1)
'2011-10-04' => string '0' (length=1)
'2011-09-27' => string '0' (length=1)
'2012-01-06' => string '0' (length=1)
), 2 => array(
'2009-05-12' => string '0' (length=1)
'2010-12-07' => string '0' (length=1)
'2010-12-02' => string '0' (length=1)
'2010-11-11' => string '0' (length=1)
'2010-09-01' => string '0' (length=1)
'2010-08-24' => string '0' (length=1)
'2010-06-30' => string '0' (length=1)
'2010-09-08' => string '0' (length=1)
'2011-09-28' => string '0' (length=1)
'2011-09-21' => string '0' (length=1)
'2012-04-10' => string '0' (length=1)
'2012-04-02' => string '0' (length=1)
'2010-12-15' => string '12' (length=2)
'2010-12-08' => string '12' (length=2)
'2010-11-28' => string '12' (length=2)
'2010-06-30' => string '12' (length=2)
'2010-05-04' => string '1' (length=1)
'2011-12-21' => string '9' (length=1)
'2012-01-05' => string '23' (length=2)
'2010-12-14' => string '0' (length=1)
'2010-12-10' => string '0' (length=1)
'2010-11-29' => string '0' (length=1)
'2011-10-04' => string '0' (length=1)
'2011-09-27' => string '0' (length=1)
'2012-01-06' => string '0' (length=1)
), 3 => array(
'2009-05-12' => string '0' (length=1)
'2010-12-07' => string '0' (length=1)
'2010-12-02' => string '0' (length=1)
'2010-11-11' => string '0' (length=1)
'2010-09-01' => string '0' (length=1)
'2010-08-24' => string '0' (length=1)
'2010-06-30' => string '0' (length=1)
'2010-09-08' => string '0' (length=1)
'2011-09-28' => string '0' (length=1)
'2011-09-21' => string '0' (length=1)
'2012-04-10' => string '0' (length=1)
'2012-04-02' => string '0' (length=1)
'2010-12-15' => string '0' (length=1)
'2010-12-08' => string '0' (length=1)
'2010-11-28' => string '0' (length=1)
'2010-06-30' => string '0' (length=1)
'2010-05-04' => string '0' (length=1)
'2011-12-21' => string '0' (length=1)
'2012-01-05' => string '0' (length=2)
'2010-12-14' => string '2' (length=1)
'2010-12-10' => string '4' (length=1)
'2010-11-29' => string '4' (length=1)
'2011-10-04' => string '7' (length=1)
'2011-09-27' => string '11' (length=2)
'2012-01-06' => string '1' (length=1)
)
);
答案 0 :(得分:1)
不要使用array_merge。
foreach($data as $index => $v){
foreach($data as $data2){
foreach($data2 as $date=> $val){
if(!array_key_exist($date,$data[$index])){
$data[$index][$date] = 0;
}
}
}
}