dongying195959 2014-11-26 20:18
浏览 67
已采纳

PHP如何对相同键的数组的值求和

This Question might seem duplicate, but I swear to have tried and tried thousands of solutions for many hours now...

I have got an associative multidimentional array like this:

    1 => 
    array (size=1)
      'Pld' => 
        array (size=2)
          'score_good_answers' => string '1' (length=1)
          'score_bad_answers' => string '0' (length=1)
  2 => 
    array (size=1)
      'Aln' => 
        array (size=2)
          'score_good_answers' => string '0' (length=1)
          'score_bad_answers' => string '1' (length=1)
  3=> 
    array (size=1)
      'IPS' => 
        array (size=2)
          'score_good_answers' => string '1' (length=1)
          'score_bad_answers' => string '0' (length=1)
 4 => 
    array (size=1)
      'Pld' => 
        array (size=2)
          'score_good_answers' => string '1' (length=1)
          'score_bad_answers' => string '0' (length=1)
  5 => 
    array (size=1)
      'Aln' => 
        array (size=2)
          'score_good_answers' => string '1' (length=1)
          'score_bad_answers' => string '0' (length=1)
 6 => 
        array (size=1)
          'Aln' => 
            array (size=2)
              'score_good_answers' => string '1' (length=1)
              'score_bad_answers' => string '0' (length=1)

FOR Var_Export:

1=> array ( 'Pld' => array ( 'score_good_answers' => '1', 'score_bad_answers' => '0', ), ), 2 => array ( 'Aln' => array ( 'score_good_answers' => '0', 'score_bad_answers' => '1', ), ), 3 => array ( 'IPS' => array ( 'score_good_answers' => '1', 'score_bad_answers' => '0', ), ), 4 => array ( 'Pld' => array ( 'score_good_answers' => '1', 'score_bad_answers' => '0', ), ),  5 => array ( 'Aln' => array ( 'score_good_answers' => '1', 'score_bad_answers' => '0', ), ),  6 => array ( 'Aln' => array ( 'score_good_answers' => '1', 'score_bad_answers' => '0', ), ),

I need to SUM the all the 'score_good_answers' and the 'score_bad_answers' for all Alns and Plds and so forth.

The Worse case scenario is that, this keys i.e: are changeable values.

At this level, whatever solution that works will be well appreciated.

I tried the Accepted Answer in this SO Question: How to sum values of the array of the same key?

However, It seems to throw several Errors....

And I Tried Several More Solutions from several SO Questions, i.e: How to sum values of the array of the same key?, Array sum of value based on same key, How to sum values via the same key and group by other key and many more...

An other close one was this:How to sum same array in php

And tried:

$array2 = array();
    for($f = 0; $f<count($getCategories); $f++){

        foreach($getCategories[$i] as $k=>$v) {
    if(!isset($array2[$v['Aln']])) {
        $array2[$v['Aln']] = $v;
    } else {
        $array2[$v['Aln']]['score_good_answers'] += $v['score_good_answers'];
    }
}

        }   var_dump($array2);

I still get Several Errors including invalid arguments undefined offsets and many more

Humbly request for any suggestion.

Thank you very much

  • 写回答

3条回答 默认 最新

  • drt12345678 2014-11-26 20:44
    关注

    You can use this code:

    $answers = array();
    
    foreach ($getCategories as $categories){
    
        foreach($categories as $category => $scores){
    
            foreach ($scores as $type => $score){
    
                if (isset($answers[$category][$type])){
                    $answers[$category][$type] += (int) $score;
                } else {
                    $answers[$category][$type] = (int) $score;
                }
    
            }
    
        }
    
    }
    

    The output of will be the following array:

    Array
    (
        [Pld] => Array
            (
                [score_good_answers] => 2
                [score_bad_answers] => 0
            )
    
        [Aln] => Array
            (
                [score_good_answers] => 2
                [score_bad_answers] => 1
            )
    
        [IPS] => Array
            (
                [score_good_answers] => 1
                [score_bad_answers] => 0
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)