dongluanban3536
2018-05-31 10:41
浏览 138
已采纳

如何计算PHP中相同数组值的总和

I have an array like this

$estimate[0]=>
'gear' =>'MMG'
'total' =>  315
   'efforts' => 9
   'afh' => 18

$estimate[1]=>
    'gear' =>'MMG'
    'total' =>  400
       'efforts' => 2
       'afh' => 6

$estimate[2]=>
    'gear' =>'BOO'
    'total' =>  200
       'efforts' => 20
       'afh' => 16

$estimate[3]=>
    'gear' =>'BOB'
    'total' =>  250
       'efforts' => 20
       'afh' => 16

I want to calculate the sum of total, efforts and afh in which gear is same and it will be stored in the another array. Following my coding is working when the array (estimate) size is less than 5.

$calculate = array();   
for($et=0;$et<count($estimate);):   
if($et==0):
    $calculate[$et]['gear'] = $estimate[$et]['gear'];
    $calculate[$et]['total'] = $estimate[$et]['total'];
    $calculate[$et]['efforts'] = $estimate[$et]['efforts'];
    $calculate[$et]['afh'] = $estimate[$et]['afh'];                 
    goto loopend;
endif;
for($cet=0;$cet<count($calculate);$cet++):
    if($estimate[$et]['gear'] == $calculate[$cet]['gear']):
        $calculate[$cet]['total'] = $calculate[$cet]['total'] + $estimate[$et]['total'];
        $calculate[$cet]['efforts'] = $calculate[$cet]['efforts'] + $estimate[$et]['efforts'];
        $calculate[$cet]['afh']    = $calculate[$cet]['afh'] + $estimate[$et]['afh'];                       
        goto loopend;   
    endif;
endfor;
    $calculate[$et]['gear'] = $estimate[$et]['gear'];
    $calculate[$et]['total'] = $estimate[$et]['total'];
    $calculate[$et]['efforts'] = $estimate[$et]['efforts'];
    $calculate[$et]['afh'] = $estimate[$et]['afh'];                 
    goto loopend;
loopend:$et++;  
endfor; 

The coding is not working more than many gears. Sometimes it works. I can't find the issues. Please help me to solve the issues.

图片转代码服务由CSDN问答提供 功能建议

我有一个这样的数组

  $ estimate [0  ] =&gt; 
'gear'=&gt;'MMG'
'total'=&gt;  315 
'努力'=&gt;  9 
'afh'=&gt;  18 
 
 $ estimate [1] =&gt; 
'gear'=&gt;'MMG'
'total'=&gt;  400 
'努力'=&gt;  2 
'afh'=&gt;  6 
 
 $ estimate [2] =&gt; 
'gear'=&gt;'BOO'
'total'=&gt;  200 
'努力'=&gt;  20 
'afh'=&gt;  16 
 
 $ estimate [3] =&gt; 
'gear'=&gt;'BOB'
'total'=&gt;  250 
'努力'=&gt;  20 
'afh'=&gt;  16 
   
 
 

我想计算总计努力 afh 其中齿轮相同,它将存储在另一个数组中。 当我的编码在数组(估计)大小小于5时工作。

  $ calculate = array();  
for($ et = 0; $ et&lt; count($ estimate);):
if($ et == 0):
 $ calculate [$ et] ['gear'] = $ estimate [$ et] [  'gear']; 
 $ calculate [$ et] ['total'] = $ estimate [$ et] ['total']; 
 $ calculate [$ et] ['effort'] = $ estimate [$ et  ] ['努力']; 
 $计算[$ et] ['afh'] = $ estimate [$ et] ['afh'];  
 goto loopend; 
endif; 
for($ cet = 0; $ cet&lt; count($ calculate); $ cet ++):
 if($ estimate [$ et] ['gear'] == $ calculate [$  cet] ['gear']):
 $ calculate [$ cet] ['total'] = $ calculate [$ cet] ['total'] + $ estimate [$ et] ['total']; 
 $ 计算[$ cet] ['努力'] = $计算[$ cet] ['努力'] + $估计[$ et] ['努力']; 
 $计算[$ cet] ['afh'] = $ 计算[$ cet] ['afh'] + $ estimate [$ et] ['afh'];  
转到loopend;  
 endif; 
endfor; 
 $ calculate [$ et] ['gear'] = $ estimate [$ et] ['gear']; 
 $ calculate [$ et] ['total'] = $ estimate [  $ et] ['total']; 
 $ calculate [$ et] ['effort'] = $ estimate [$ et] ['effort']; 
 $ calculate [$ et] ['afh'] = $ 估计[$等] [ 'AFH'];  
 goto loopend; 
loopend:$ et ++;  
endfor;  
   
 
 

编码不是比许多齿轮工作的。 有时它有效。 我找不到问题。 请帮我解决问题。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • drmcm84800 2018-05-31 12:12
    已采纳

    You might use array_reduce:

    $result = array_reduce($estimate, function($carry, $item) {
        if (!isset($carry[$item["gear"]])) {
            $carry[$item["gear"]] = $item;
            return $carry;
        }
    
        $carry[$item["gear"]]["total"] += $item["total"];
        $carry[$item["gear"]]["efforts"] += $item["efforts"];
        $carry[$item["gear"]]["afh"] += $item["afh"];
    
        return $carry;
    });
    

    Demo

    点赞 评论
  • dswwuo1223 2018-05-31 10:56
      <?php 
    
      $new_arr = array();
      $estimate[0] =array(
        'gear' =>'MMG',
        'total' =>  315,
        'efforts' => 9,
        'afh' => 18
      );
      $estimate[1]=array(
        'gear' =>'MMG',
        'total' =>  400,
        'efforts' => 2,
        'afh' => 6,
      );
      $estimate[2]=array(
        'gear' =>'BOO',
        'total' =>  200,
        'efforts' => 20,
        'afh' => 16,
      );
      $estimate[3]=array(
        'gear' =>'BOB',
        'total' =>  250,
        'efforts' => 20,
        'afh' => 16,
      );
    
      foreach ($estimate as $key => $value) {
       $new_arr[$value['gear']] = array(
          'total'   =>  (isset($new_arr[$value['gear']]['total']) ? ($new_arr[$value['gear']]['total']  + $value['total']) : $value['total'] ),
          'efforts' =>  (isset($new_arr[$value['gear']]['efforts']) ? ($new_arr[$value['gear']]['efforts']  + $value['efforts']) : $value['efforts'] ),
          'afh'     =>  (isset($new_arr[$value['gear']]['afh']) ? ($new_arr[$value['gear']]['afh']  + $value['afh']) : $value['afh'] )
       );
      }
    
    
      echo "<pre>";print_r($new_arr);
    
    点赞 评论
  • dongqian3198 2018-05-31 11:00

    As per my comment use foreach loop when your array length is not define

    Here is your desired code

       <?php
     $estimate = array( 
                "0" => array (
                   "gear" => 35,
                   "total" => 30,   
                   "efforts" => 39,
                   "afh" => 39,
                ),
    
               "1" => array (
                   "gear" => 35,
                   "total" => 30,   
                   "efforts" => 39,
                   "afh" => 39,
                ),
    
                "2" => array (
                   "gear" => 35,
                   "total" => 30,   
                   "efforts" => 39,
                   "afh" => 39,
                ),
             );
     $gear=0;
     $total=0;
     $efforts=0;
     $afh=0;
     foreach ($estimate as $key => $value) {
            $gear=$gear+$value['gear'];
            $total=$gear+$value['total'];
            $efforts=$gear+$value['efforts'];
            $afh=$gear+$value['afh'];
     }
     echo "<pre>";
     $result = array('gear' => $gear, 'total' => $total,'efforts' => $efforts,'afh' => $afh);
     echo "<pre>";
    print_r($result);
    

    you can check the result HERE

    点赞 评论

相关推荐 更多相似问题