我有一个像这样的数组
0 =>
array (size=3)
'datetime_read' => '2017-07-16 20:45:08'
'parameter_name' => 'Rainfall Amount'
'reading' => '0.00'
1 =>
array (size=3)
'datetime_read' => '2017-07-16 20:45:08'
'parameter_name' => 'Rainfall Intensity'
'reading' => '0.0'
2 =>
array (size=3)
'datetime_read' => '2017-07-16 20:45:08'
'parameter_name' => 'Air Pressure'
'reading' => '77.45'
我需要按日期对数据进行分组,并获取参数名称和读取值
[0] => Array
(
'datetime_read' => '2017-07-16 20:45:08'
'Rainfall Amount' => '0.00'
'Rainfall Intensity' => '0.0'
'Air Pressure' => '77.45'
)
答案 0 :(得分:0)
有多种方法可以解决这个问题。这就是我要解决的问题:
答案 1 :(得分:0)
试试这个(假设$input
是您的输入):
$combined = array_reduce(
$input,
function (array $carry, array $item) {
$datetimeRead = $item['datetime_read'];
$parameterName = $item['parameter_name'];
$reading = $item['reading'];
$carry[$datetimeRead['datetime_read'] = $datetimeRead;
$carry[$datetimeRead][$parameterName] = $reading;
return $carry;
},
[]
);
ksort($combined);
$data = array_values($combined);
供参考,见: