-1

我需要合并一个多维数组,以便在合并后新值等于 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)
)

);

4

1 回答 1

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;
          }
       } 
    }
}
于 2012-12-03T15:50:58.433 回答