It's 3am here, i'm super tired and I have no idea how to make this thing to work. The following array is returned from database and contains info like page views, date and messages for the last 7 days:
Array
(
[0] => Array
(
[vws] => 9
[mss] => 0
[dt] => 2016-06-27
)
[1] => Array
(
[vws] => 4
[mss] => 0
[dt] => 2016-06-26
)
[2] => Array
(
[vws] => 1
[mss] => 0
[dt] => 2016-06-24
)
[3] => Array
(
[vws] => 7
[mss] => 0
[dt] => 2016-06-23
)
)
This array should contain data even if there are no page views or messaged so this is what i'm looking for:
Array
(
[0] => Array
(
[vws] => 0
[mss] => 0
[dt] => 2016-06-22
)
[1] => Array
(
[vws] => 13
[mss] => 1
[dt] => 2016-06-23
)
[2] => Array
(
[vws] => 7
[mss] => 3
[dt] => 2016-06-24
)
[3] => Array
(
[vws] => 45
[mss] => 10
[dt] => 2016-06-25
)
[4] => Array
(
[vws] => 0
[mss] => 0
[dt] => 2016-06-26
)
[5] => Array
(
[vws] => 0
[mss] => 0
[dt] => 2016-06-27
)
[6] => Array
(
[vws] => 5
[mss] => 1
[dt] => 2016-06-28
)
)
Sorry if I was not clear enough. I will update my question if you need more info. Thank you in advance
EDIT
It seems that using dt
value as a key is a good idea but I still think that the same result could be achieved more easily by checking dt
value with in_array
.
Thanks to @ArtisiticPhoenix , this code works just fine for now:
$new = [];
foreach ($data as $k => $v) {
$new[$v['dt']] = $v;
}
$Date = new DateTime();
$days = [];
for ($i = 0; $i < 7; ++$i) {
$days[$Date->format('Y-m-d')] = [
'vws' => 0,
'mss' => 0,
'dt' => $Date->format('Y-m-d')
];
$Date->modify('-1 days');
}
$final = array_merge($days, $new);