3v4l.org

run code in 300+ PHP versions simultaneously
<?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);
Output for git.master, git.master_jit, rfc.property-hooks
array(14) { [0]=> array(2) { ["date"]=> string(10) "2017-09-01" ["total"]=> int(4) } [1]=> array(2) { ["date"]=> string(10) "2017-09-02" ["total"]=> int(0) } [2]=> array(2) { ["date"]=> string(10) "2017-09-03" ["total"]=> int(0) } [3]=> array(2) { ["date"]=> string(10) "2017-09-04" ["total"]=> int(0) } [4]=> array(2) { ["date"]=> string(10) "2017-09-05" ["total"]=> int(0) } [5]=> array(2) { ["date"]=> string(10) "2017-09-06" ["total"]=> int(0) } [6]=> array(2) { ["date"]=> string(10) "2017-09-07" ["total"]=> int(6) } [7]=> array(2) { ["date"]=> string(10) "2017-09-08" ["total"]=> int(0) } [8]=> array(2) { ["date"]=> string(10) "2017-09-09" ["total"]=> int(0) } [9]=> array(2) { ["date"]=> string(10) "2017-09-10" ["total"]=> int(0) } [10]=> array(2) { ["date"]=> string(10) "2017-09-11" ["total"]=> int(0) } [11]=> array(2) { ["date"]=> string(10) "2017-09-12" ["total"]=> int(6) } [12]=> array(2) { ["date"]=> string(10) "2017-09-13" ["total"]=> int(0) } [13]=> array(2) { ["date"]=> string(10) "2017-09-14" ["total"]=> int(6) } }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
165.45 ms | 409 KiB | 5 Q