dongying195959 2014-11-26 12:18
浏览 68
已采纳

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 12: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部