0

I got this array and I am trying to make a sum of the [za] values but without any success.

My last try was this code:

foreach ( $sumza as $sumzakey ) {
              $sum += $sumza[$sumzakey]['za'];
}





Array
(
    [0] => Array
        (
            [za] => 3600
        )

    [1] => Array
        (
            [za] => 7200
        )

    [2] => Array
        (
            [za] => -27360
        )

    [3] => Array
        (
            [za] => 3600
        )

    [4] => Array
        (
            [za] => 
        )

    [5] => Array
        (
            [za] => 3600
        )

    [6] => Array
        (
            [za] => 3600
        )

    [7] => Array
        (
            [za] => 3600
        )

    [8] => Array
        (
            [za] => 7200
        )

    [9] => Array
        (
            [za] => 7740
        )

    [10] => Array
        (
            [za] => 
        )

    [11] => Array
        (
            [za] => -27360
        )

    [12] => Array
        (
            [za] => 7200
        )

    [13] => Array
        (
            [za] => 3600
        )

    [14] => Array
        (
            [za] => 8640
        )

    [15] => Array
        (
            [za] => 3600
        )

    [16] => Array
        (
            [za] => 6840
        )

    [17] => Array
        (
            [za] => 
        )

    [18] => Array
        (
            [za] => 5040
        )

    [19] => Array
        (
            [za] => 5040
        )

    [20] => Array
        (
            [za] => 5040
        )

    [21] => Array
        (
            [za] => -27360
        )

    [22] => Array
        (
            [za] => 
        )

)

1 Answer 1

2

Try:

$sum = 0;
foreach ( $sumza as $sumzakey ) {
    $sum += $sumzakey['za'];
}

$sumzakey actually represents each array within $sumza. If you wanted to use your approach you would do:

$sum = 0;
foreach ( $sumza as $sumzakey => $sumzavalue ) {
    $sum += $sumza[$sumzakey]['za'];
}

In that case you now have the array key for each array element in $sumza and can use it to access each array element in $sumza. But that would be unecessary and not as clear as the code above.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.