adding end values in a multidimensional array

jack

I have an array with the following structure.

Is there a function which can combine elements together, summing the totals?

Thanks for any pointers on how to achieve this. I've included a section of the array here, and then lower down the hopeful result.

I can achieve this via creating a second array as I reinterate over a php mysql query, but this doesn't feel like a very efficient way of doing things.

Array
(
    [1] => Array
        (
            [Y01MA] => Array
                (
                    [O] => Array
                        (
                            [F] => Array
                                (
                                    [1] => 4
                                    [2] => 6
                                    [3] => 1
                                )

                            [M] => Array
                                (
                                    [1] => 5
                                    [2] => 5
                                )

                        )

                    [P] => Array
                        (
                            [F] => Array
                                (
                                    [2] => 4
                                    [3] => 6
                                    [4] => 1
                                )

                            [M] => Array
                                (
                                    [1] => 1
                                    [2] => 4
                                    [3] => 5
                                )

                        )

                )

            [Y01MB] => Array
                (
                    [O] => Array
                        (
                            [F] => Array
                                (
                                    [1] => 7
                                    [2] => 2
                                )

                            [M] => Array
                                (
                                    [1] => 11
                                    [2] => 1
                                    [3] => 1
                                )

                        )

                    [P] => Array
                        (
                            [F] => Array
                                (
                                    [2] => 3
                                    [3] => 6
                                )

                            [M] => Array
                                (
                                    [2] => 3
                                    [3] => 9
                                    [4] => 1
                                )

                        )

                )

        )

Ideally what I would then end up with is something like this

Array ( [1] => Array ( [O] => Array ( [F] => Array ( [1] => 18 [2] => 2 [3] => 0 [4] => 0 )

                [M] => Array
                    (
                        [1] => 23
                        [2] => 0
                        [3] => 0
                        [4] => 0
                    )

            )

        [P] => Array
            (
                [F] => Array
                    (
                        [1] => 1
                        [2] => 11
                        [3] => 7
                        [4] => 1
                    )

                [M] => Array
                    (
                        [1] => 4
                        [2] => 11
                        [3] => 8
                        [4] => 0
                    )

            )

    )
Jakub Matczak

You need a recursive sum of array.

You can achieve this by a recursively reducing array using array_reduce function.

function sumRecursive($carry, $item) {
    if(is_array($item)){
        return array_reduce($item, 'sumRecursive', $carry);
    }
    return $carry+$item ;
}
$sum = sumRecursive(0, $arr);

Here's example working code: https://3v4l.org/FuGtf

Other way would to be utilize RecursiveArrayIterator and RecursiveIteratorIterator to iterate over the first one. ;-)

$sum = array_sum(
    iterator_to_array(
        new RecursiveIteratorIterator(
            new RecursiveArrayIterator($arr)
        ),
        false
    )
);

Working code: https://3v4l.org/gEB6q

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Pushing values into multidimensional array

Extracting values from multidimensional array

Counting Values in Multidimensional Array PHP

Grouping and adding values multidimensional array

find repeated values in multidimensional array

adding values to multidimensional aray in PHP

PHP - efficient way of adding multidimensional array values to a specific key

group unique values in multidimensional array

Add array values with key to multidimensional array without adding a new 'layer'

Adding elements to the Multidimensional Array

PHP - Looping through and adding values to array, loop back if end is reached to add remaining values

Adding an element to the end of an array

Adding to a multidimensional array from another array

Renaming values in multidimensional array

Counting multidimensional array values

Adding a key to a multidimensional array from another multidimensional array

Assigning values in multidimensional array

array keys as values with multidimensional array

Multidimensional array sum values

Modify values of array / multidimensional array

How to Insert array at the end of into multidimensional array?

Adding values to a multidimensional associative array in php

multidimensional array, comparing the values

Print multidimensional array's end-values with full keys path

Adding object to multidimensional array

PHP 7.1: Adding values to multidimensional arrays

Adding values to the end of each index of an array in php

Adding values to multidimensional array PHP without overwritting last added value

Printing multidimensional array values