-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 に答える