PHP计算嵌套数组的总数

时间:2017-08-16 07:48:15

标签: php arrays multidimensional-array

嗨,我有这个示例数组。我想计算总面积,然后将其添加到父总计加上位置的总计。因此,第一个位置总数将是" 11"因为7 + 4.然后"状态"父母也会这样做10(第一州父母)+11(第一个孩子)+10(第二个孩子)= 31。

    $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();
        }

2 个答案:

答案 0 :(得分: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;
}

希望它有所帮助。

答案 1 :(得分:0)

对Gopal的致谢(谢谢你的队友)。只是简单地修改了@ Gopalkrishna的格式化答案:

enter image description here