<?php $arr = array ( 0=>array( 'date'=>'2017-09-01', 'total'=>4 ), 1=>array( 'date'=>'2017-09-07', 'total'=>6 ), 2=>array( 'date'=>'2017-09-12', 'total'=>6 ), 3=>array( 'date'=>'2017-09-14', 'total'=>6 ) ); // array with only dates to search in $dates = array_column($arr, "date"); // Create an array with all dates from first item to last item $start = new DateTime($arr[0]["date"]); $end = new DateTime(end($arr)["date"]); $range = new DatePeriod($start, new DateInterval('P1D'), $end); // $range is now all dates from start to end minus last one. // Loop through the range foreach($range as $date){ //See if the current date exist is first array $find = array_search($date->format("Y-m-d"), $dates); If($find !== false){ $result[] = $arr[$find]; // if it does copy it to result array }Else{ // If not add it and create a total = 0 $result[] = array('date' => $date->format("Y-m-d"), 'total' => 0); } } // Since the loop does not loop all dates we need to add the last item to result. $result[] = end($arr); Var_dump($result);
You have javascript disabled. You will not be able to edit any code.