php - Split an array in sub arrays of consecutive dates -
for rent application, have array $dates this:
array ( [2013-07-19] => 1 [2013-07-21] => 3 [2013-07-23] => 2 [2013-07-24] => 4 [2013-07-25] => 4 [2013-07-26] => 2 [2013-07-27] => 2 [2013-07-30] => 3 [2013-07-31] => 1 ) the date key, , values number of items rent in day specific product
how can split array in many sub arrays containing each list of consecutive days? this:
array ( [0] => array ( [2013-07-19] => 1 ) [1] => array ( [2013-07-21] => 3 ) [2] => array ( [2013-07-23] => 2 [2013-07-24] => 4 [2013-07-25] => 4 [2013-07-26] => 2 [2013-07-27] => 2 ) [3] => array ( [2013-07-30] => 3 [2013-07-31] => 1 ) )
you can this:
$data = array( '2013-07-19' => 1, '2013-07-21' => 3, '2013-07-23' => 2, '2013-07-24' => 4, '2013-07-25' => 4, '2013-07-26' => 2, '2013-07-27' => 2, '2013-07-30' => 3, '2013-07-31' => 1 ); $result = array(); $ref = new datetime('1821-11-11'); foreach ($data $datum => $nb) { if ($ref->add(new dateinterval('p1d'))->format('y-m-d')!=$datum) { $result[] = array(); $ref = new datetime($datum); } $result[array_pop(array_keys($result))][$datum] = $nb; } print_r($result);
Comments
Post a Comment