I have an initial array of dates:
Array ( [0] => 2015-11-23 [1] => 2015-11-24 [2] => 2015-11-25 [3] => 2015-11-26 [4] => 2015-11-27 [5] => 2015-11-28 [6] => 2015-11-29 )
and an another array of "results":
Array (
[0] => stdClass Object ( [order_date] => 2015-11-24 [SUM(order_total)] => 1458.5 )
[1] => stdClass Object ( [order_date] => 2015-11-25 [SUM(order_total)] => 1540 )
[2] => stdClass Object ( [order_date] => 2015-11-26 [SUM(order_total)] => 1256 )
[3] => stdClass Object ( [order_date] => 2015-11-27 [SUM(order_total)] => 2516.5 )
[4] => stdClass Object ( [order_date] => 2015-11-28 [SUM(order_total)] => 3436.5 ) )
How can I create a new array that returns the dates from the initial array and their corresponding value from the results array, if it exists, but if not return 0?
So far, if $dates is the date array and $day_of_week_array is the results array, I have tried this:
foreach ($dates as $date) {
echo $date."<br>";
foreach($day_of_week_array as $day_data){
$day_date = 'order_date';
$total = 'SUM(order_total)';
if ($date === $day_of_week_array->$day_date) {
echo $day_data->$total."<br>";
}
else { }
}
}
However this seems rather convoluted and does not allow me to return 0 if the dates don't match (as the array is nested) , therefore defeating the purpose of the exercise.