I am trying to populate an array so that I can plot a chart using chart.js
I have an array that looks like:
[0] => Array
(
[id] => 1
[date] => 09-04-2018
[length] => 10
)
[1] => Array
(
[id] => 2
[date] => 09-04-2018
[length] => 20
)
[2] => Array
(
[id] => 1
[date] => 10-04-2018
[length] => 11
)
[3] => Array
(
[id] => 2
[date] => 10-04-2018
[length] => 21
)
[4] => Array
(
[id] => 1
[date] => 11-04-2018
[length] => 12
)
[5] => Array
(
[id] => 1
[date] => 12-04-2018
[length] => 13
)
[6] => Array
(
[id] => 2
[date] => 12-04-2018
[length] => 23
)
[7] => Array
(
[id] => 1
[date] => 13-04-2018
[length] => 14
)
I need to split this array into 2 datasets (id 1 and id 2) however I cannot have gaps in these datasets as chartJS does not like it.
I need the array to look like:
[1] => Array
(
[09-04-2018] => 10
[10-04-2018] => 11
[11-04-2018] => 12
[12-04-2018] => 13
[13-04-2018] => 14
)
[2] => Array
(
[09-04-2018] => 20
[10-04-2018] => 21
[11-04-2018] => 0
[12-04-2018] => 23
[13-04-2018] => 0
)
using the following code:-
foreach ($array as $item)
{
$id = $item['id'];
$date = $item['date'];
$length = $item['length'];
$output[$id][$date] = $length;
}
I can produce an array in the format I like but as the initial array is missing 2 dates for dataset 2 I do not get these added in and the result is:-
[1] => Array
(
[09-04-2018] => 10
[10-04-2018] => 11
[11-04-2018] => 12
[12-04-2018] => 13
[13-04-2018] => 14
)
[2] => Array
(
[09-04-2018] => 20
[10-04-2018] => 21
[12-04-2018] => 23
)
is there a way to populate the array the way I want so if 1 of the datasets uses that date, both(all it maybe 3 or 4 datasets) have to use that date but by simply adding in a 0?