0

I cant get my head around how to do this.

Consider I have the following php array

Array
(
    [0] => Array
        (
            [cat_id] => 2
            [parent_id] => 1
            [Title] => Default Category
            [Product Count] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [cat_id] => 4
                            [parent_id] => 2
                            [Title] => computers & electronics
                            [Product Count] => 5
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [cat_id] => 5
                                            [parent_id] => 4
                                            [Title] => projectors
                                            [Product Count] => 2
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [cat_id] => 6
                                                            [parent_id] => 5
                                                            [Title] => film projectors
                                                            [Product Count] => 1
                                                        )

                                    [1] => Array
                                        (
                                            [cat_id] => 7
                                            [parent_id] => 4
                                            [Title] => projector cases
                                            [Product Count] => 0
                                            children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [cat_id] => 8
                                                            [parent_id] => 7
                                                            [Title] => leather cases
                                                            [Product Count] => 10

I want to somehow iterate over each array element and total up the Product Count element based on its on value and all child values (which could be n levels deep)

The end result I am looking for is

Array
(
    [0] => Array
        (
            [cat_id] => 2
            [parent_id] => 1
            [Title] => Default Category
            [Product Count] => 18
            [children] => Array
                (
                    [0] => Array
                        (
                            [cat_id] => 4
                            [parent_id] => 2
                            [Title] => computers & electronics
                            [Product Count] => 18
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [cat_id] => 5
                                            [parent_id] => 4
                                            [Title] => projectors
                                            [Product Count] => 3
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [cat_id] => 6
                                                            [parent_id] => 5
                                                            [Title] => film projectors
                                                            [Product Count] => 1
                                                        )

                                    [1] => Array
                                        (
                                            [cat_id] => 7
                                            [parent_id] => 4
                                            [Title] => projector cases
                                            [Product Count] => 10
                                            children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [cat_id] => 8
                                                            [parent_id] => 7
                                                            [Title] => leather cases
                                                            [Product Count] => 10

can anyone help please? Speed or efficiency is not really a major issue as this will be run once a day at 3 in the morning so no-one will be waiting for it to finish its work!!

1
  • you can write a recursive code which checks if child array exists and if it does then get the product count, add the product count and again check if child array exists. Commented Jun 24, 2015 at 15:34

1 Answer 1

1

The only reasonable way to tally up values in an array with unknown depth is by using recursive methods.

function sumItems(&$branch,$sum){

    $sum = $branch['Product Count'];
    foreach($branch['children'] as &$item){
          $sum += sumItems($item,$sum); // Each sub array runs this function
                                        // And adds thier count to the parent branch
    }

    $branch['Product Count'] = $sum; // Since branch is passed to function as
                                    // Reference, this line overrides the original value

    return $sum; // Each recursion of this function returns its own value + the sum of all sub arrays to the parent
}
sum_items($array,0); // Will return the sum of all branches and sub branches
Sign up to request clarification or add additional context in comments.

4 Comments

Made some changes, should work or require fine tuning. Let me know if you need clarification on any of parts of the example
Thanks for the reply. I cant get my head around how to use this though - must be Wednesday afternoon thing! Say my array is in the variable $tree. How would I call this function to total up all of the elements and the sub arrays etc in $tree.
Fixed a bug and added comments. Hope you can make it worl
Thank you Skarlinksi :-) I had to call it using sum_items($array[0],0); There was also an errant } which was just above return $sum; which I have edited out - just waiting on peer review. Many thanks for your help

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.