多维数组,对同一个key的值求和
Multi dimensional array, sum values of the same key
我有一个多维数组。我需要用相同的 'driver' 和 'date' 键
来总结距离值
Input :
$firstArr = array(
0 =>array('driver'=>'xxxx',
'distance' => 100,
'vehicle' => 1,
'date' => '2019-10'),
1=>array('driver'=>'xxxx',
'distance' => 200,
'vehicle' => 2,
'date' => '2019-10'),
2=>array('driver'=>'yyyy',
'distance' => 100,
'vehicle' => 3,
'date' => '2019-10'));
预期输出:
$finalArr = array(
0 =>array('driver'=>'xxxx',
'distance' => 300,
'vehicle' => '1,2',
'date' => '2019-10'),
1=>array('driver'=>'yyyy',
'distance' => 100,
'vehicle' => 3,
'date' => '2019-10'));
我试过下面的代码。但输出是一个按日期排列的数组。
$subArr = array();
foreach($firstArr as $key => $val){
$subArr[$val['driver']][$val['date']]['distance'] += $val['distance'];
$subArr[$val['driver']][$val['date']]['vehicle'] .= $val['vehicle'].',';
$subArr[$val['driver']][$val['date']]['date'] = $val['date'];
$subArr[$val['driver']][$val['date']]['driver'] = $val['driver'];
}
$result = array_values($subArr);
您可以使用dirver
和date
作为键,Demo
$result = [];
foreach($array as $v){
$key = $v["driver"] . "_" . $v["date"];
if(isset($result[$key])){
$result[$key]["distance"] += $v["distance"];
}else{
$result[$key] = $v;
}
}
$result = array_values($result);
您也可以使用array_reduce按driver和日期汇总距离和车辆。
在 array_combine 的帮助下,我们可以为每个 driver 日期组合设置初始数组(如果不存在),然后对所需字段进行处理。
$keys = array_keys($input[0]);
$result = array_reduce($input, function ($sum, $row) use ($keys) {
$key = $row['driver'] . $row['date'];
$sum[$key] = $sum[$key] ?? array_combine($keys, [$row['driver'], null, null, $row['date']]);
$sum[$key]['distance'] += $row['distance'];
$sum[$key]['vehicle'] .= (($sum[$key]['vehicle']) ? ',': '' ) . $row['vehicle'];
return $sum;
}, []);
结果:
Array
(
[xxxx2019-10] => Array
(
[driver] => xxxx
[distance] => 300
[vehicle] => 1,2
[date] => 2019-10
)
[yyyy2019-10] => Array
(
[driver] => yyyy
[distance] => 100
[vehicle] => 3
[date] => 2019-10
)
)
我有一个多维数组。我需要用相同的 'driver' 和 'date' 键
来总结距离值Input :
$firstArr = array(
0 =>array('driver'=>'xxxx',
'distance' => 100,
'vehicle' => 1,
'date' => '2019-10'),
1=>array('driver'=>'xxxx',
'distance' => 200,
'vehicle' => 2,
'date' => '2019-10'),
2=>array('driver'=>'yyyy',
'distance' => 100,
'vehicle' => 3,
'date' => '2019-10'));
预期输出:
$finalArr = array(
0 =>array('driver'=>'xxxx',
'distance' => 300,
'vehicle' => '1,2',
'date' => '2019-10'),
1=>array('driver'=>'yyyy',
'distance' => 100,
'vehicle' => 3,
'date' => '2019-10'));
我试过下面的代码。但输出是一个按日期排列的数组。
$subArr = array();
foreach($firstArr as $key => $val){
$subArr[$val['driver']][$val['date']]['distance'] += $val['distance'];
$subArr[$val['driver']][$val['date']]['vehicle'] .= $val['vehicle'].',';
$subArr[$val['driver']][$val['date']]['date'] = $val['date'];
$subArr[$val['driver']][$val['date']]['driver'] = $val['driver'];
}
$result = array_values($subArr);
您可以使用dirver
和date
作为键,Demo
$result = [];
foreach($array as $v){
$key = $v["driver"] . "_" . $v["date"];
if(isset($result[$key])){
$result[$key]["distance"] += $v["distance"];
}else{
$result[$key] = $v;
}
}
$result = array_values($result);
您也可以使用array_reduce按driver和日期汇总距离和车辆。 在 array_combine 的帮助下,我们可以为每个 driver 日期组合设置初始数组(如果不存在),然后对所需字段进行处理。
$keys = array_keys($input[0]);
$result = array_reduce($input, function ($sum, $row) use ($keys) {
$key = $row['driver'] . $row['date'];
$sum[$key] = $sum[$key] ?? array_combine($keys, [$row['driver'], null, null, $row['date']]);
$sum[$key]['distance'] += $row['distance'];
$sum[$key]['vehicle'] .= (($sum[$key]['vehicle']) ? ',': '' ) . $row['vehicle'];
return $sum;
}, []);
结果:
Array
(
[xxxx2019-10] => Array
(
[driver] => xxxx
[distance] => 300
[vehicle] => 1,2
[date] => 2019-10
)
[yyyy2019-10] => Array
(
[driver] => yyyy
[distance] => 100
[vehicle] => 3
[date] => 2019-10
)
)