adding end values in a multidimensional array











up vote
-2
down vote

favorite












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
)

)

)









share|improve this question
























  • Do you need to group them in any way at all or just every value in the array. What have you tried so far?
    – Nigel Ren
    Nov 8 at 11:12










  • ideally I'd like to be able to work out how many [1] [2] [3] [4] there are for each [M] and [F]
    – jack
    Nov 8 at 11:15










  • Can you give a clear example of the output you want in the question.
    – Nigel Ren
    Nov 8 at 11:18















up vote
-2
down vote

favorite












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
)

)

)









share|improve this question
























  • Do you need to group them in any way at all or just every value in the array. What have you tried so far?
    – Nigel Ren
    Nov 8 at 11:12










  • ideally I'd like to be able to work out how many [1] [2] [3] [4] there are for each [M] and [F]
    – jack
    Nov 8 at 11:15










  • Can you give a clear example of the output you want in the question.
    – Nigel Ren
    Nov 8 at 11:18













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











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
)

)

)









share|improve this question















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
)

)

)






php arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 12:41

























asked Nov 8 at 11:09









jack

104




104












  • Do you need to group them in any way at all or just every value in the array. What have you tried so far?
    – Nigel Ren
    Nov 8 at 11:12










  • ideally I'd like to be able to work out how many [1] [2] [3] [4] there are for each [M] and [F]
    – jack
    Nov 8 at 11:15










  • Can you give a clear example of the output you want in the question.
    – Nigel Ren
    Nov 8 at 11:18


















  • Do you need to group them in any way at all or just every value in the array. What have you tried so far?
    – Nigel Ren
    Nov 8 at 11:12










  • ideally I'd like to be able to work out how many [1] [2] [3] [4] there are for each [M] and [F]
    – jack
    Nov 8 at 11:15










  • Can you give a clear example of the output you want in the question.
    – Nigel Ren
    Nov 8 at 11:18
















Do you need to group them in any way at all or just every value in the array. What have you tried so far?
– Nigel Ren
Nov 8 at 11:12




Do you need to group them in any way at all or just every value in the array. What have you tried so far?
– Nigel Ren
Nov 8 at 11:12












ideally I'd like to be able to work out how many [1] [2] [3] [4] there are for each [M] and [F]
– jack
Nov 8 at 11:15




ideally I'd like to be able to work out how many [1] [2] [3] [4] there are for each [M] and [F]
– jack
Nov 8 at 11:15












Can you give a clear example of the output you want in the question.
– Nigel Ren
Nov 8 at 11:18




Can you give a clear example of the output you want in the question.
– Nigel Ren
Nov 8 at 11:18












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










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






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206543%2fadding-end-values-in-a-multidimensional-array%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    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






    share|improve this answer

























      up vote
      1
      down vote



      accepted










      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






      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        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






        share|improve this answer












        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 11:31









        Jakub Matczak

        11.7k43448




        11.7k43448






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206543%2fadding-end-values-in-a-multidimensional-array%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown