Calculation and summation of php two-dimensional array

 [34] => Array
        (
            [sum] => 13
            [name] => 
            [id] => 27
            [parentid] => 1
            [children] => Array
                (
                )

        )

    [35] => Array
        (
            [sum] => 2
            [name] => 
            [id] => 31
            [parentid] => 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [sum] => 156
                            [name] => 
                            [id] => 52
                            [parentid] => 31
                            [children] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [sum] => 12
                            [name] => 
                            [id] => 54
                            [parentid] => 31
                            [children] => Array
                                (
                                )

                        )

                    [2] => Array
                        (
                            [sum] => 20
                            [name] => 
                            [id] => 53
                            [parentid] => 31
                            [children] => Array
                                (
                                )

                        )

                    [3] => Array
                        (
                            [sum] => 10
                            [name] => 
                            [id] => 55
                            [parentid] => 31
                            [children] => Array
                                (
                                )

                        )

                    [4] => Array
                        (
                            [sum] => 55
                            [name] => 
                            [id] => 51
                            [parentid] => 31
                            [children] => Array
                                (
                                )

                        )

                )

        )

    [36] => Array
        (
            [sum] => 56
            [name] => 
            [id] => 32
            [parentid] => 1
            [children] => Array
                (
                )

        )

how to calculate the summ value of the third branch is the sum of all the sum under the children of the third branch

Php
Apr.21,2021

$arr = [[
  'name' => '',
  'children' => [
    ['sum' => 156],
    ['sum' => 12]
  ]
]];

function getChildrenSum($children)
{
  $sum = 0;
  if (is_array($children)) {
    foreach ($children as $item) {
      $sum += $item['sum'];
      if(isset($item['children'])){
        $sum += getChildrenSum($item['children']);
      }
    }
  }
  return $sum;
}

$sum = array_reduce(
  array_filter($arr, function ($item) {
    return $item['name'] === '';
  }),
  function ($sum, $item) {
    return $sum + getChildrenSum($item['children']);
  },
  0
);

echo $sum;

$data = Array
(
    34 => Array
    (
        'sum'     => 13,
        'name'    => '',
        'id'      => 27,
        'parentid'=> 1,
        'children' => Array
        (
        ),
    ),
    35 => Array(
        'sum'     => 2,
        'name'    => '',
        'id'      => 31,
        'parentid'=> 1,
        'children' => Array
        (
            0 => Array
            (
                'sum'     => 156,
                'name'    => '',
                'id'      => 52,
                'parentid'=> 31,
                'children'=> [],

            ),
            1 => Array
            (
                'sum'     => 12,
                'name'    => '',
                'id'      => 54,
                'parentid'=> 31,
                'children'=> [],
            ),
        ),
    ),
    36 => Array
    (
        'sum'     => 56,
        'name'    => '',
        'id'      => 32,
        'parentid'=> 1,
        'children' => Array
        (
        ),
    )
);

foreach($data as $key => $value)
{
    $data[$key]['sum'] = array_sum(array_column($value['children'],'sum'));
}
print_r($data);

foreach calculation is fine. It's not the infinite pole


that uses foreach loops.


define a variable: $sums = 0;
assume that the large array outside you is $data,
foreach ($data as $key = > $value)
{

if($value['name'] == ''){

        foreach($value['children'] as $k => $v)
        {
           $sums += $v['sum']  
        }
    }

}

print_r ($sums);

)

or:
foreach ($data as $key = > $value)
{

$sums = array_sum(array_column($value['children'],'sum'));

}
print_r ($sums);

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ead6dc-2155.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ead6dc-2155.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?