关联数组,相同键的和值

时间:2013-01-07 12:25:42

标签: php arrays sum associative-array

所以我有这个关联数组(使用kint完成转储)

dump done with Kint d

而不是让密钥“Conference”重复3次。我想只让它一次,并将3个值合并为一个,以便得到类似的东西:

会议:4534

对于重复的所有其他键也是如此..

是否有可以做到的原生功能?

5 个答案:

答案 0 :(得分:28)

你可以尝试

$data = array(
  0 => array(
    'event' => 'Conference',
    'budget' => 3700,
  ),
  1 => array(
    'event' => 'Conference',
    'budget' => 500,
  ),
  2 => array(
    'event' => 'Showroom',
    'budget' => 1000,
  ),
  3 => array(
    'event' => 'Mission Chez client',
    'budget' => 2000,
  ),
  4 => array(
    'event' => 'Séminaire',
    'budget' => 700,
  ),
  5 => array(
    'event' => 'Livraison',
    'budget' => 4000,
  ),
  6 => array(
    'event' => 'Conference',
    'budget' => 334,
  ),
);

$sum = array_reduce($data, function ($a, $b) {
    isset($a[$b['event']]) ? $a[$b['event']]['budget'] += $b['budget'] : $a[$b['event']] = $b;  
    return $a;
});


print_r(array_values($sum));

输出

Array
(
    [0] => Array
        (
            [event] => Conference
            [budget] => 4534
        )

    [1] => Array
        (
            [event] => Showroom
            [budget] => 1000
        )

    [2] => Array
        (
            [event] => Mission Chez client
            [budget] => 2000
        )

    [3] => Array
        (
            [event] => Séminaire
            [budget] => 700
        )

    [4] => Array
        (
            [event] => Livraison
            [budget] => 4000
        )

)

答案 1 :(得分:4)

一个简单的建议:

$results = array();
foreach ($budgetByEventTemp as $value)
{
  if( ! isset($results[$value['event']]) )
  {
     $results[$value['event']] = 0;
  }

  $results[$value['event']] += $value['budget'];

}

var_dump($results);
根据评论

更新

你可以再次运行它们:

foreach($results as $key => $value)
{
  $structured_results[] = array('event' => $key, 'budget' => $value);
}

var_dump($structured_results);

答案 2 :(得分:2)

$sumArray = array();

foreach ($myArray as $k=>$subArray) {
foreach ($subArray as $id=>$value) {
  $sumArray[$id]+=$value;
  }
}

print_r($sumArray);

答案 3 :(得分:0)

此选项将对数组中所有重复索引的值进行分组和求和。

此处的代码:

$aValues[]=array("nametogroup",10);
$aValues[]=array("nametogroup",20);
$aValues[]=array("nametogroup2",30);
$aValues[]=array("nametogroup2",20);
echo var_dump($aValues); // array before grouping
foreach ($aValues as  $id=>$value) 
{
  $a2sum["{$value[0]}"]=$value[1] + $a2sum["{$value[0]}"];
}
echo var_dump($a2sum); //array after group and adding values

这将导致:

array
0 => 
array
  0 => string nametogroup (length=11)
  1 => int 10
1 => 
array
  0 => string nametogroup (length=11)
  1 => int 20
2 => 
array
  0 => string nametogroup2 (length=12)
  1 => int 30
3 => 
array
  0 => string nametogroup2 (length=12)
  1 => int 20

array
nametogroup => int 30
nametogroup2 => int 50

答案 4 :(得分:0)

要迭代一组数据并返回可能较小的一组数据,array_reduce() 是一个明智的“函数式”选择。这是@Baba 使用空合并运算符和更直观的变量命名约定的答案的轻微变化。

代码:(Demo)

var_export(
    array_reduce(
        $data,
        function ($carry, $row) {
            $carry[$row['event']] = ($carry[$row['event']] ?? 0) + $row['budget'];
            return $carry;
        }
    )
);