在数组中填写缺少的日期和价格(例如周末)

时间:2013-01-22 09:41:48

标签: php arrays multidimensional-array

我有一个像这样“差距”的数组:

$arr = array ( 
"bananas" => array (
    "2010-01-01" => "10.1",
    "2010-01-02" => "11.4", 
    "2010-01-05" => "13.3", //missing 03, 04
    "2010-01-06" => "09.2",
    "2010-01-07" => "11.1" ),
"grapes" => array (
    "2011-01-05" => "20.1",
    "2011-01-06" => "21.4",
    "2011-01-07" => "23.3",
    "2011-01-08" => "29.2",
    "2011-01-10" => "21.1", //missing 09
    "2011-01-11" => "21.0" ),
"coconuts" => array (
    "2012-01-28" => "50.1",
    "2012-01-29" => "51.4", 
    "2012-02-02" => "53.3", //missing 30, 31, 01
    "2012-02-03" => "51.1" ) 
);

我不知道阵列中有多少产品和“date =>价格” 如何在差距之前用最后价格填写缺失的日期?

重要提示:日期可以在不同的月份(请查看"coconuts")。

我希望结果如下:

$arr = array ( 
"bananas" => array (
    "2010-01-01" => "10.1",
    "2010-01-02" => "11.4", //was last price before gap
    "2010-01-03" => "11.4", //filling missing
    "2010-01-04" => "11.4", //filling missing
    "2010-01-05" => "13.3", 
    "2010-01-06" => "09.2",
    "2010-01-07" => "11.1" ),
"grapes" => array (
    "2011-01-05" => "20.1",
    "2011-01-06" => "21.4",
    "2011-01-07" => "23.3",
    "2011-01-08" => "29.2", //was last price before gap
    "2011-01-09" => "29.2", //filling missing
    "2011-01-10" => "21.1", 
    "2011-01-11" => "21.0" ),
"coconuts" => array (
    "2012-01-28" => "50.1", 
    "2012-01-29" => "51.4", //was last price before gap
    "2012-01-30" => "51.4", //filling missing
    "2012-01-31" => "51.4", //filling missing
    "2012-02-01" => "51.4", //filling missing
    "2012-02-02" => "53.3",
    "2012-02-03" => "51.1" ) 
);

感谢您的时间。

2 个答案:

答案 0 :(得分:1)

<?php
$arr = array ( 
"bananas" => array (
    "2010-01-01" => "10.1",
    "2010-01-02" => "11.4", 
    "2010-01-05" => "13.3", //missing 03, 04
    "2010-01-06" => "09.2",
    "2010-01-07" => "11.1" ),
"grapes" => array (
    "2011-01-05" => "20.1",
    "2011-01-06" => "21.4",
    "2011-01-07" => "23.3",
    "2011-01-08" => "29.2",
    "2011-01-10" => "21.1", //missing 09
    "2011-01-11" => "21.0" ),
"coconuts" => array (
    "2012-01-28" => "50.1",
    "2012-01-29" => "51.4", 
    "2012-02-02" => "53.3", //missing 30, 31, 01
    "2012-02-03" => "51.1" ) 
);  

$result=array();
while(list($name,$data)=each($arr))
{
   $result[$name]=array();
   if(!empty($data))
   {
      $last_price=reset($data);
      $cur_key=key($data);
      $curdate=new DateTime($cur_key);

      while(list($k, $price)=each($data))
      {
         $dt=new DateTime($k);
         while($curdate<=$dt)
         {
            $cd=$curdate->format('Y-m-d');
            if($curdate!=$dt)  $result[$name][$cd]=$last_price;
            else               $result[$name][$cd]=$price;
            $curdate->modify('+1 day');
         }
         $last_price=$price;
      }
   }
}

print_r($result);
?>

答案 1 :(得分:0)

foreach ($arr as $fruit => $data)
{
    $previousprice = 0;
    $previoustime = false;

    foreach ($data as $day => $price)
    {
        $time = strtotime($day);

        if ($previoustime && (($time - $previoustime) > 86400))
            $data[date('Y-m-d', ($previoustime + 86400))] = $previousprice;

        $previousprice = $price;
        $previoustime = $time;
    }

    ksort($data);
}