簡體   English   中英

如何在PHP中計算相同數組值的總和

[英]how to calculate the sum of same array values in PHP

我有這樣的數組

$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的總和,並將其存儲在另一個數組中。 當數組(估計)的大小小於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; 

編碼的工作原理並不比許多齒輪好。 有時可以。 我找不到問題。 請幫我解決問題。

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

根據我的評論,當您的數組長度未定義時,請使用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);

您可以在這里查看結果

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

演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM