获取多维数组中的公共值
Get common values in a multidimension array
希望你一切顺利
我有一个多维数组,其输出如下所示
[888] => Array
(
[team_name] => freight
[total] => 7707103.66482
)
[887] => Array
(
[team_name] => freight
[total] => 6954528.9139
)
[890] => Array
(
[team_name] => assembly line
[total] => 1953
)
我想获取一个单独数组中的所有索引,这些索引具有共同的 team_name。
也许这样的事情可以完成这项工作:
$result = [];
foreach($yourArray as $ar) {
if (isset($result[$ar['team_name]])) {
$result[$ar['team_name']] += $ar['total'];
} else {
$result[$ar['team_name']] = $ar['total'];
}
}
$result-array 应该看起来像这样(虽然没有测试!):
array(
'freight' => 7707103.66482 + 6954528.9139,
'assebly line' => 1953
)
希望你一切顺利 我有一个多维数组,其输出如下所示
[888] => Array
(
[team_name] => freight
[total] => 7707103.66482
)
[887] => Array
(
[team_name] => freight
[total] => 6954528.9139
)
[890] => Array
(
[team_name] => assembly line
[total] => 1953
)
我想获取一个单独数组中的所有索引,这些索引具有共同的 team_name。
也许这样的事情可以完成这项工作:
$result = [];
foreach($yourArray as $ar) {
if (isset($result[$ar['team_name]])) {
$result[$ar['team_name']] += $ar['total'];
} else {
$result[$ar['team_name']] = $ar['total'];
}
}
$result-array 应该看起来像这样(虽然没有测试!):
array(
'freight' => 7707103.66482 + 6954528.9139,
'assebly line' => 1953
)