2017-08-16 134 views
-3

嗨,我有这个示例数组。我想计算总面积,然后将其加到父母总数加上地点的个人总数。所以第一个地点的总数将是“11”,因为7 + 4。那么“国家”父母也会这样做10(第一个国家父母)+11(第一个孩子)+10(第二个孩子)= 31.PHP计算嵌套数组总数

$arr = array(
     array(
      'type' => 'state', 'total' => '10', 
      'location' => array(
       array(
        'type' => 'location', 'total' => '4', 
        'area' => array(
         array('type' => 'area', 'total' => '6'), 
         array('type' => 'area', 'total' => '1') 
        ) 
       ), 
       array(
        'type' => 'location', 'total' => '5', 
        'area' => array(
         array('type' => 'area', 'total' => '2'), 
         array('type' => 'area', 'total' => '3') 
        ) 
       ) 
      ) 
     ), 

     array(
      'type' => 'state', 'total' => '20', 
      'location' => array(
       array(
        'type' => 'location', 'total' => '4', 
        'area' => array(
         array('type' => 'area', 'total' => '8'), 
         array('type' => 'area', 'total' => '7') 
        ) 
       ) 
      ) 
     ) 
    ); 

所需的正确的输出现在应该重新创建:

$arr_FINAL = array(
     array(
      'type' => 'state', 'total' => '31', 
      'location' => array(
       array(
        'type' => 'location', 'total' => '11', 
        'area' => array(
         array('type' => 'area', 'total' => '6'), 
         array('type' => 'area', 'total' => '1') 
        ) 
       ), 
       array(
        'type' => 'location', 'total' => '10', 
        'area' => array(
         array('type' => 'area', 'total' => '2'), 
         array('type' => 'area', 'total' => '3') 
        ) 
       ) 
      ) 
     ), 

     array(
      'type' => 'state', 'total' => '39', 
      'location' => array(
       array(
        'type' => 'location', 'total' => '19', 
        'area' => array(
         array('type' => 'area', 'total' => '8'), 
         array('type' => 'area', 'total' => '7') 
        ) 
       ) 
      ) 
     ) 
    ); 

在持续未完成的解决方案为每CBroe要求:

 // country > state > location > area 
     foreach ($arr as $k => $v) { 
      foreach ($v['location'] as $k2 => $v2) { 
       foreach ($v2['area'] as $k3 => $v3) { 
        echo $v3['total'] . ","; 
        $ctr_area[] = $v3['total']; 

       } 
       $arr2[$k]['location'][$k2]['total'] += array_sum($ctr_area); 
       $ctr_location[] = $v2['total']; 
       $ctr_area = array(); 
      } 
      $arr2[$k]['total'] += array_sum($ctr_location); 

      $ctr_state[] = $v['total']; 
      $ctr_location = array(); 
     } 
+0

这是数据的一个非常具体的设置,可能会需要特定的功能。我不会浪费时间和精力制定动态解决方案。首先让它工作,然后让它变得更好。 – DarkMukke

+1

所以,你只是在这里放弃你的要求,甚至没有表现出丝毫的方法...是,“你挑战复杂的逻辑问题的激情” - 你谈论你的个人资料? – CBroe

+0

@CBroe我是多任务处理。我发布了这个问题,然后继续这样做。我目前的解决方案到目前为止是“检查更新的问题”:) – marknt15

回答

2

试试这个:

for($i = 0; $i < count($arr); $i++){ 
$state_total = $arr[$i]["total"];  
for($j = 0; $j < count($arr[$i]['location']); $j++){ 
    $location_total = $arr[$i]['location'][$j]["total"]; 
    for($k = 0; $k < count($arr[$i]['location'][$j]['area']); $k++){ 
     if(isset($arr[$i]['location'][$j]['area'][$k])){ 
      $location_total = $location_total + $arr[$i]['location'][$j]['area'][$k]['total']; 
     } 
    } 
    $arr[$i]['location'][$j]["total"] = $location_total; 
    $state_total = $state_total + $location_total; 
} 
$arr[$i]["total"] = $state_total; 
} 

希望它有帮助。

+0

感谢这@Gopalkrishna它的工作!干杯! :) – marknt15

0

学分Gopal(感谢伴侣)。只是做了@ Gopalkrishna的回答简单的修订,格式:

enter image description here