It's been a long time since i asked a question here, because most of the answers to my questions can already be found.
But today i need help
I'm working with these set of data array:
array (
'_metadata' =>
array (
'from' => '2018-11-30T16:00:00+00:00',
'to' => '2018-12-08T11:05:40+00:00',
'granularity' => 'DAY',
'aggregation' => 'SUM',
),
'metrics' =>
array (
'DOWN_PUBLIC' =>
array (
'unit' => 'B',
'values' =>
array (
0 =>
array (
'value' => 23501281,
'timestamp' => '2018-12-02T16:05:00+00:00',
),
1 =>
array (
'value' => 221147881,
'timestamp' => '2018-12-03T16:05:00+00:00',
),
2 =>
array (
'value' => 104525623,
'timestamp' => '2018-12-04T16:05:00+00:00',
),
3 =>
array (
'value' => 163306933,
'timestamp' => '2018-12-05T16:05:00+00:00',
),
4 =>
array (
'value' => 159806208,
'timestamp' => '2018-12-06T16:05:00+00:00',
),
5 =>
array (
'value' => 122203658,
'timestamp' => '2018-12-07T16:05:00+00:00',
),
),
),
'UP_PUBLIC' =>
array (
'unit' => 'B',
'values' =>
array (
0 =>
array (
'value' => 111801,
'timestamp' => '2018-12-02T16:05:00+00:00',
),
1 =>
array (
'value' => 5594252,
'timestamp' => '2018-12-03T16:05:00+00:00',
),
2 =>
array (
'value' => 11654085,
'timestamp' => '2018-12-04T16:05:00+00:00',
),
3 =>
array (
'value' => 10870143,
'timestamp' => '2018-12-05T16:05:00+00:00',
),
4 =>
array (
'value' => 9288760,
'timestamp' => '2018-12-06T16:05:00+00:00',
),
5 =>
array (
'value' => 9288262,
'timestamp' => '2018-12-07T16:05:00+00:00',
),
),
),
),
)
I need to convert the timestamp to date (ie; 3 Dec) and the value(bytes) to convert to megabytes, then pass it to javascript as an array, so the output will be something like:
$dateConverted = ['3 Dec', '4 Dec', '5 Dec', '6 Dec', '7 Dec'];
$megabytesDown = [23.50, 221.15, 104.53, ...];
$megabytesUp = [0.11, 5.59, 11.65, ...];
Using Dexter's Answer below, cleaned it up a little got it working great
function custDateFormat($date){
return date('d M', strtotime($date));
}
function bytes_to_mb($bytes, $decimal_places = 1 ){
return number_format($bytes / 1048576, $decimal_places);
}
$bigArray = json_decode($Dataresponse);
$dateConverted = [];
$megabytesDown = [];
$megabytesUp = [];
$upVal = $bigArray->metrics->UP_PUBLIC->values;
$downVal = $bigArray->metrics->DOWN_PUBLIC->values;
foreach($bigArray->metrics->UP_PUBLIC->values as $key => $val)
{
$dateConverted[] = custDateFormat($val->timestamp);
$megabytesDown[] = bytes_to_mb($downVal[$key]->value);
$downTotal+= bytes_to_mb($downVal[$key]->value);
$megabytesUp[] = bytes_to_mb($upVal->value);
$upTotal+= bytes_to_mb($upVal->value);
}
Passing the results to Chart.js, outcome: