首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在PHP中计算相同数组值的和

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

Stack Overflow用户
提问于 2018-05-31 18:41:39
回答 3查看 116关注 0票数 0

我有一个这样的数组

$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

我想计算totaleffortsafh的总和,其中gear是相同的,它将存储在另一个数组中。当数组(估计)大小小于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; 

编码并不比许多齿轮工作得更好。有时它是有效的。我找不到问题所在。请帮我解决这些问题。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-05-31 20:12:54

您可以使用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

票数 1
EN

Stack Overflow用户

发布于 2018-05-31 18:56:51

  <?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);
票数 1
EN

Stack Overflow用户

发布于 2018-05-31 19:00:54

根据我的评论,在未定义数组长度时使用foreach循环

以下是您想要的代码

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

您可以检查结果HERE

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50622410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档