2014-03-05 88 views
0

嗨我有一個數組,我想在數組的不同層次上總結一級兒童的值。PHP數組中的兒童總和

<?php 
$group = Array('Electronics' => Array(
        '6 - Cameras & Supplies' => Array(
       'Cameras' => Array(
        'Camcorders' => Array (
         'Action Camcorders' => Array (
          'total_ty_yest_sales' => 70.83, 
          'total_wo_dotcom_ty_yest_sales' => 401.59, 
          'east_ty_yest_sales' => 65.20 
         ), 
         'Standard Camcorders' => Array (
          'total_ty_yest_sales' => 96.09, 
          'total_wo_dotcom_ty_yest_sales' => 96.09, 
          'east_ty_yest_sales' => 68.21 
         ), 
         'Surveillance' => Array(
          'total_ty_yest_sales' => 84.00, 
          'total_wo_dotcom_ty_yest_sales' => 84.00, 
          'east_ty_yest_sales' => 26.00 
         ) 
        ), 
        'subCameras' => Array (
         'Big Zoom' => Array(
          'total_ty_yest_sales' => 31.66, 
          'total_wo_dotcom_ty_yest_sales' => 13.68, 
          'east_ty_yest_sales' => 1.47 
         ), 
         'Pegged Cameras' => Array(
          'total_ty_yest_sales' => 13.50, 
          'total_wo_dotcom_ty_yest_sales' => 5.50, 
          'east_ty_yest_sales' => 5.00 
         ), 
         'Point-N-Shoot' => Array(
          'total_ty_yest_sales' => 46.61, 
          'total_wo_dotcom_ty_yest_sales' => 10.35, 
          'east_ty_yest_sales' => 4.06 
         ), 
         'Rugged Cameras' => Array(
          'total_ty_yest_sales' => 87.04, 
          'total_wo_dotcom_ty_yest_sales' => 87.04, 
          'east_ty_yest_sales' => 65.20 
         ), 
         'SLR' => Array(
          'total_ty_yest_sales' => 50.19, 
          'total_wo_dotcom_ty_yest_sales' => 9.40, 
          'east_ty_yest_sales' => 1.37 
         ) 
        ) 
      ) ) 
     )); 
     $totalSum = 0; 
     echo "<table>\n 
       <thead></thead>\n 
       <tbody>\n"; 

     foreach($group as $gmm => $acctg_dept_nbrs) { 
      echo "<tr class=\"header\">\n 
        <td>" . $gmm . "</td>\n"; 

      foreach ($acctg_dept_nbrs as $acctg_dept_nbr => $dept_catg_grp_descs) { 
       echo "<tr class=\"header\">\n 
         <td style=\"padding-left: 1em;\">" . $acctg_dept_nbr . "</td>\n"; 
          echo "<td style=\"width:100px;\">SUM</td>\n"; 
          echo "<td style=\"width:100px;\">SUM</td>\n"; 
          echo "<td style=\"width:100px;\">SUM</td>\n"; 
       $total_acctg_dept_nbr = 0; 
       foreach($dept_catg_grp_descs as $dept_catg_grp_desc => $dept_category_descs) { 
        echo "<tr class=\"header\">\n 
          <td style=\"padding-left: 2em;\">" . $dept_catg_grp_desc . "</td>\n"; 
          echo "<td style=\"width:100px;\">SUM</td>\n"; 
          echo "<td style=\"width:100px;\">SUM</td>\n"; 
          echo "<td style=\"width:100px;\">SUM</td>\n"; 
        $total_dept_catg_grp_desc = 0; 
        foreach($dept_category_descs as $dept_category_desc => $dept_subcatg_descs) { 
         echo "<tr class=\"header\">\n 
           <td style=\"padding-left: 3em;\">" . $dept_category_desc . "</td>\n"; 
           echo "<td style=\"width:100px;\">SUM</td>\n"; 
           echo "<td style=\"width:100px;\">SUM</td>\n"; 
           echo "<td style=\"width:100px;\">SUM</td>\n"; 

         foreach($dept_subcatg_descs as $dept_subcatg_desc => $values) { 
          echo "<tr>\n 
            <td style=\"padding-left: 4em;\">" . $dept_subcatg_desc . "</td>\n"; 

          $sum = $values['total_ty_yest_sales']; 
          echo "<td style=\"width:100px;\">".$sum."</td>\n"; 
          $sum = $values['total_wo_dotcom_ty_yest_sales']; 
          echo "<td style=\"width:100px;\">".$sum."</td>\n"; 
          $sum = $values['east_ty_yest_sales']; 
          echo "<td style=\"width:100px;\">".$sum."</td>\n"; 

         } 
        } 
       } 
      } 
     } 

?> 

我想用輸入中的「SUM」替換下一級縮進值的總和。

+0

有你看着['array_sum()'](HTTP:/ /php.net/array_sum)? –

+0

我有,但我不太明白如何在這種情況下應用它。 – user3352316

+0

我不確定如何引用每個級別的值,因爲這些值只保存在數組的最低級別。 – user3352316

回答

0

這不是做最有效的事,但你可以創建一個計算陣列遞歸之和的函數:

function array_sum_recursive($array) 
{ 
    $sum = 0; 

    array_walk_recursive($array, function($item) use (&$sum) { 
     $sum += $item; 
    }); 

    return $sum; 
}