I have 2 database objects that provide me with data over time. The results don't necessarily have the same dates however. A user could have entered just their weight one day, or body fat another day, or both on the same day.
weight_history
date - amount
Wed March 2, 2016 - 194.00
Wed March 3, 2016 - 205.40
bodyfat_history
date - amount
Wed March 1, 2016 - 24.80
Wed March 2, 2016 - 22.80
I want to graph this data so I need to merge the days that do match and then have a null in the remaining value where they don't.
The logic I'm failing to grasp is how I can add the others to get a complete data set.
Here is my code which, works for merging the data and grabbing the remaining weight results. Whats the best way to add the bodyfat results that don't match as well. If I do them inside the nested loop the data will be added multiple times.
foreach(array_reverse($weight_history->result()) as $row):
foreach(array_reverse($bodyfat_history->result()) as $row2):
if(date("F j, Y", $row->date) == date("F j, Y", $row2->date)){
$results[date("F j, Y", $row->date)]['weight'] = $row->amount;
$results[date("F j, Y", $row->date)]['bodyfat'] = $row2->amount;
}
$results[date("F j, Y", $row->date)]['weight'] = $row->amount;
endforeach;
endforeach;
Here is the result set I am after.
date - weight - bodyfat
Wed March 1, 2016 - Null - 24.80
Wed March 2, 2016 - 194.00 - 22.80
Wed March 3, 2016 - 205.40 - Null