Associative array, sum values of the same key

2020-01-25 01:16发布

So I have this associative array (dump done with kint)

dump done with Kint d

Instead of having the key "Conference" repeating 3 times. I want to have it just once and sum the 3 values into one in order to have something like:

Conference : 4534

And same thing for all other keys that are repeating..

Is there a native function that can do that ?

4条回答
淡お忘
2楼-- · 2020-01-25 01:41

A simple suggestion:

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

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

}

var_dump($results);

Update according to comments

You can run over them again:

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

var_dump($structured_results);
查看更多
Evening l夕情丶
3楼-- · 2020-01-25 01:42
$sumArray = array();

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

print_r($sumArray);
查看更多
Lonely孤独者°
4楼-- · 2020-01-25 01:52

You can try

$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));

Output

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
        )

)
查看更多
Juvenile、少年°
5楼-- · 2020-01-25 01:53

This option will group and sum values of all repeated indexes in an array.

Code here :

$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

This will result into:

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
查看更多
登录 后发表回答