0

First of all, sorry for my english.

I want to go through an array of any levels, but I want to go in the bottom level to up level and update a value of key recursively, but an example is better than a text:

this is my example code:

Array
(
    [1] => Array
        (
            [ItemText] => Home
            [ItemLink] => index.php
            [count] => 0
            [id] => 1
            [ParentID] => 
            [Children] => Array
                (
                    [2] => Array
                        (
                            [ItemText] => Home Sub 1
                            [ItemLink] => somepage.php
                            [id] => 2
                            [count] => 0
                            [ParentID] => 1
                            [Children] => Array
                                (
                                    [3] => Array
                                        (
                                            [ItemText] => Home Sub 2
                                            [ItemLink] => somepage2.php
                                            [id] => 3
                                            [count] => 1
                                            [ParentID] => 2
                                            [Children] => Array
                                                (
                                                )

                                        )

                                    [4] => Array
                                        (
                                            [ItemText] => Contact
                                            [ItemLink] => contact.php
                                            [id] => 4
                                            [count] => 1
                                            [ParentID] => 2
                                            [Children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)

note the count key in any level of array. Each level is a child of the "current" position. I need this:

Array
(
    [1] => Array
        (
            [ItemText] => Home
            [ItemLink] => index.php
            [count] => **2**
            [id] => 1
            [ParentID] => 
            [Children] => Array
                (
                    [2] => Array
                        (
                            [ItemText] => Home Sub 1
                            [ItemLink] => somepage.php
                            [id] => 2
                            [count] => **2**
                            [ParentID] => 1
                            [Children] => Array
                                (
                                    [3] => Array
                                        (
                                            [ItemText] => Home Sub 2
                                            [ItemLink] => somepage2.php
                                            [id] => 3
                                            [count] => 1
                                            [ParentID] => 2
                                            [Children] => Array
                                                (
                                                )

                                        )

                                    [4] => Array
                                        (
                                            [ItemText] => Contact
                                            [ItemLink] => contact.php
                                            [id] => 4
                                            [count] => 1
                                            [ParentID] => 2
                                            [Children] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)

I want to accumulate and sum the count of all child in the current position and go through the prev level and accumulate again all count of the current level and that's to the up level.

I appreciate all your help. Thanks in advance.

EDIT

I adapted the function of @HamzaKubba to my needs and this works for me. I put this for those to require:

function explore(& $node) {
    $count = 0;
    if (count($node) > 0) {
        foreach ($node as &$value) {
            if (!isset($value['count']))
                $value['count'] = 0;

            if (count($value['Children']) > 0)
                $value['count'] += explore($value['Children']);

            $count += $value['count'];
        }
    }
    return $count;
}
4
  • What have you tried and what didn't work? We are not your personal scripting army. Commented Oct 15, 2013 at 3:16
  • Your example doesn't make a whole lot of sense, to the point where it makes the whole question ambiguous. Why is there a **2** at the root? Commented Oct 15, 2013 at 3:23
  • Should be **3** at the top level, right? One child, 2 grand-children? If so, my answer below is what you want... Commented Oct 15, 2013 at 3:30
  • thanks to all for your answers. The 2 at the root is fine. The child has two (2) "products". The parent is the category that group all subcategories inside. In this case this is only has 2 products. There is it to the top level. What if I make myself understood? Commented Oct 15, 2013 at 10:48

1 Answer 1

2

pseudoCode of general template:

function explore(node) {
    foreach (child of node) {
         explore(child)
    }
    // now that we're done with children, do logic/calculation here
    doSomething(node)
}

pseudoCode of what you want (from what I understand):

function explore(node) {
    foreach (child of node) {
         node.count = node.count + explore(child)
    }
    return node.count
}

to correct your new code:

function explore_($node) {
    if (!isset($node['count']))
        $node['count'] = 0;

    foreach ($node['Children'] as $child) {
        $node['count'] = $node['count'] + explore_($child);
    }

    return $node['count'];
}
Sign up to request clarification or add additional context in comments.

6 Comments

This is a good template function for a depth first tree processor.
thanks @HamzaKubba for your help. But, I don't need accumulate the number of 'Children' inside any level of array. I need group the Children and count value of 'Children' key and the parent is the "category" but the user can create any category inside the category that he wants. So, that what I need is count the number of products inside a category but the parent level sum all 'count' value and put this value in the array parent. I dont know if I did understand?
Try to solve it using the general template I gave you. Then if you're still stuck, come back and show us the code so we can help you out.
@HamzaKubba, I edit the question. Can you help me with this, please?
@HamzaKubba, I modify the content of my question and I use the code that you suggested me, but It didn't work for me. Could you help me with this please?
|

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.