自从我在这里问一个问题以来已经很久了,因为我的问题的大多数答案都已经找到。
但是今天我需要帮助
我正在使用这些数据数组集:
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',
),
),
),
),
)
我需要将时间戳转换为日期(即12月3日),并将值(字节)转换为兆字节,然后将其作为数组传递给javascript,所以输出将类似于:
$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, ...];
使用下面的Dexter的答案,将其清理一点,使其运行良好
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);
}
将结果传递到Chart.js,结果:
答案 0 :(得分:0)
function custDateFormat($date){
return date('d M', strtotime($date));
}
function convertToReadableSize($size){
$base = log($size) / log(1024);
return round(pow(1024, $base - floor($base)), 1);
}
$dateConverted = [];
$megabytesDown = [];
$megabytesUp = [];
$upVal = $bigArray['UP_PUBLIC']['values'];
$downVal = $bigArray['DOWN_PUBLIC']['values'];
foreach($bigArray['UP_PUBLIC']['values'] as $key => $val)
{
$dateConverted[] = custDateFormat($val['timestamp']);
$megabytesDown[] = convertToReadableSize($downVal[$key]['value']);
$megabytesUp[] = convertToReadableSize($val['value']);
}