Home > other >  How to count in multidimensional array
How to count in multidimensional array

Time:01-15

i'm having issues with multidimensional array:

 Array
(
    [start] => Array
        (
            [0] => [email protected]
            [1] => [email protected]
            [2] => [email protected]
            [3] => [email protected]
            [4] => [email protected]
            [5] => [email protected]
            [6] => bot001e08@lopbot001
            [7] => [email protected]
            [8] => [email protected]
            [9] => [email protected]
        )

    [name] => Array
        (
            [0] => 43
            [1] => 17
            [2] => 15
            [3] => 34
            [4] => 12
            [5] => 37
            [6] => 14
            [7] => 12
            [8] => 33
            [9] => 25
        )

)

This multi array has the same number of keys. I would like to show all different emails (start array) and count all values from name array.

So the desired output would be something like that:

[email protected]: 43 34 37 23 = 137

[email protected]: 17 15 12 = 44

[email protected]: 12

bot001e08@lopbot001: 14

[email protected]: 25

With special function i could get result like this:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [[email protected]] => 43
                    [[email protected]] => 17
                    [[email protected]] => 15
                )

            [1] => Array
                (
                    [[email protected]] => 34
                    [[email protected]] => 12
                )

            [2] => Array
                (
                    [[email protected]] => 37
                    [bot001e08@lopbot001] => 14
                    [[email protected]] => 12
                )

            [3] => Array
                (
                    [[email protected]] => 33
                    [[email protected]] => 25
                )

        )

)

But i still can not count the values from this multi array

CodePudding user response:

Use this simple loop with nested indexes

$mails = array();
        foreach ($data['start'] as $key => $value) {
            $mails[$value]['sum'] = @$mails[$value]['sum']   $data['name'][$key];
        }
    
 print_r($mails);

Result:

(
[[email protected]] => Array
    (
        [sum] => 147
    )

[[email protected]] => Array
    (
        [sum] => 44
    )

[[email protected]] => Array
    (
        [sum] => 12
    )

[bot001e08@lopbot001] => Array
    (
        [sum] => 14
    )

[[email protected]] => Array
    (
        [sum] => 25
    )

)

  •  Tags:  
  • Related