Search code examples
phparraysmultidimensional-arraysumgrouping

Sum of deep column values in a multidimensional array


I have this array:

Array ( [0] => 
       Array ( 
        [0] => Array ( 
          [07] => Array ( 
               [2] => 352.9
               [3] => 375737
               [4] => 1000000002
           ) ) 
        [1] => Array ( 
           [07] => Array ( 
               [2] => 362.1
               [3] => 375797
               [4] => 1000000002
        ) ) 
)
Array ( [1] => 
        [0] => Array ( 
           [08] => Array ( 
               [2] => 305.7
               [3] => 375857
               [4] => 1000000002
        ) )
    )
)

i need a final array sum for the key 07 ( is the month ) like this:

Array ( [0] => 
     Array ( 
            [0] => Array ( 
              [07] => Array ( 
                   [2] => 3254.9 ( the sum of each 07 )
                   [3] => 6521545 ( the sum )
                   [4] => 98474916521621 ( the sum )
               ) ) 
)
Array ( [1] => 
            [0] => Array ( 
              [08] => Array ( 
                   [2] => 305.7 ( not summed cause month 08 is only one )
                   [3] => 375857 ""
                   [4] => 1000000002 ""
               ) ) 
    )
)

Any help?


Solution

  • Here, try this - I'm sure it's neither a perfect nor optimal solution ( 3 foreach-es ), but it works on a reasonably large data set...

    $inputArray is the multidimensional array with the data you provided, btw...

    EDIT: Fixed version:

    $result = array();
    
    foreach( $inputArray as $subArray ) {
    
        foreach ( $subArray as $subKey => $member ) {
    
            if ( empty( $result[$subKey]) ) {
    
                $result[$subKey] = $member;
    
            } else {
                foreach ( $member as $id => $subMember ) {
    
                    if ( empty( $result[$subKey][$id]) ) {
    
                        $result[$subKey][$id] = $subMember;
    
                    } else {
    
                        $result[$subKey][$id] += $subMember;
    
                    }
                }
            }
        }
    }
    

    EDIT2: Since you changed the format of arrays, the solution is different:

    Note: $array1 and $array2 are your "global" - predefined arrays.

    $arrayWrapper = array_merge( ( array ) $array1, ( array ) $array2 );
    
    $result = array();
    
    foreach ( $arrayWrapper as $inputArray ) {
    
        foreach( $inputArray as $subArray ) {
    
            foreach ( $subArray as $subKey => $member ) {
    
                if ( empty( $result[$subKey]) ) {
    
                    $result[$subKey] = $member;
    
                } else {
                    foreach ( $member as $id => $subMember ) {
    
                        if ( empty( $result[$subKey][$id]) ) {
    
                            $result[$subKey][$id] = $subMember;
    
                        } else {
    
                            $result[$subKey][$id] += $subMember;
    
                        }
                    }
                }
            }
        }
    }
    

    Tested it with your data, should work.

    Cheers.