Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
Can you show the code with which it's not working? Or do you want us to write it from scratch? – Yoshi Aug 3 '11 at 8:13
Are you sure your figures are correct? The sums in 07 seem a little to arbitrary to draw any conclusion. – aefxx Aug 3 '11 at 8:17
i need to write it from scratch!! – Ste Aug 3 '11 at 9:23

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
I have try this, but do not work! i see the same array if i print_r $result! – Ste Aug 3 '11 at 10:26
Sorry, I tested it and fixed it. Now, it should work fine. – Nemanja Aug 3 '11 at 11:39
Thx! work! I have edited my question adding 2 global arrays! can u fix code for that way? I can't understand how do it!! – Ste Aug 3 '11 at 13:45
I rewrote the answer to fit your new data format. Cheers. – Nemanja Aug 3 '11 at 14:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.