0

Okay here is my recursion algo:

public function getCategoryTree($tree,$return = array()) {
    foreach ($tree->children as $child) {
      if (count($child->children) > 0 )
        $return[$tree->name] = $this->getCategoryTree($child, $return);
      else
        $return[] = $child->name;
    } 
    return $return;
  }

Here is a snippet of the data structure I'm trying to traverse

Object(stdClass)#290 (6) {
      ["category_id"]=>
      int(1)
      ["parent_id"]=>
      int(0)
      ["name"]=>
      string(4) "Root"
      ["position"]=>
      int(0)
      ["level"]=>
      int(0)
      ["children"]=>
      array(2) {
        [0]=>
        object(stdClass)#571 (7) {
          ["category_id"]=>
          int(2)
          ["parent_id"]=>
          int(1)
          ["name"]=>
          string(18) "Root MySite.com"
          ["is_active"]=>
          int(0)
          ["position"]=>
          int(0)
          ["level"]=>
          int(1)
          ["children"]=>
          array(11) {
            [0]=>
            object(stdClass)#570 (7) {
              ["category_id"]=>
              int(15)
              ["parent_id"]=>
              int(2)
              ["name"]=>
              string(9) "Widgets"
              ["is_active"]=>
              int(1)
              ["position"]=>
              int(68)
              ["level"]=>
              int(2)
              ["children"]=>
              array(19) {
                [0]=>
                object(stdClass)#566 (7) {
                  ["category_id"]=>
                  int(24)
                  ["parent_id"]=>
                  int(15)
                  ["name"]=>
                  string(16) "Blue widgets"
                  ["is_active"]=>
                  int(1)
                  ["position"]=>
                  int(68)
                  ["level"]=>
                  int(3)
                  ["children"]=>
                  array(0) {
                  }
                }

<snip....>

I'm trying to get a php data structure like such

categories = array( "Root" =>
                  array("Root MySite.com" =>
                    array( "Widgets" =>
                         // final element is NOT an array
                         array ("Blue Widgets", "Purple Widgets" ...) 
                    )
                  )
              )

I can't quite seem to get the data structure i'm looking for using my recursive algo. Any help would be great.

Eventually I'll need to parse it again on the frontend and display it, but another problem for another day...

4
  • 1
    When did it become "cool" to use algo? Commented Jan 10, 2013 at 18:03
  • sometime in the last 2 years when the word 'epic' became popular Commented Jan 10, 2013 at 18:05
  • It is not clear to me "I'm trying to store it in Mongo DB as such". So you need a way to convert your object to a string??? objects can be saved as strings when they are serialized. There are many ways in php to do this. The two most significant are: json-encode and serialize Commented Jan 10, 2013 at 18:19
  • luis I edited for clarity, the mongo db part is irrelevant, just need to the proper php data structure Commented Jan 10, 2013 at 18:23

1 Answer 1

2

Have a look at this phpFiddle for a full working example. The only error I found was the $this->getCategoryTree which gave me an Fatal Error Using $this when not in object context. So are you sure the function is within the correct scope?

Updated

I hope this one works. :)

function traverse($root, $return = array()) {
    $return[$root->name] = array();
    foreach ($root->children as $child) {
        if (count($child->children) > 0) {
            traverse($child, &$return[$root->name]);
        }else {
            array_push(&$return[$root->name], $child->name);
        }
    }
    return $return;
}

The output from this is:

Array ( [Root] => 
    Array ( 
        [Root MySite.com] => 
            Array ( 
                [Widgets] => Array ( [0] => Blue Widget [1] => Purple Widget) 
                [Gizmos] => Array ( [0] => Blue Gizmos [1] => Purple Gizmos) 
            ) 
        [FooBar.com] => 
            Array ( 
                [Widgets] => Array ( [0] => Blue Widget [1] => Purple Widget) 
                [Gizmos] => Array ( [0] => Blue Gizmos [1] => Purple Gizmos) 
            ) 
    ) 
)

Again, full working example

Sign up to request clarification or add additional context in comments.

7 Comments

+1 for your working fiddle, but array_push is just the same that [], so the question is again to @bonez, what is wrong?
Thanks. Finally understood what you meant. Yeah, the only error I found when using his function was the use $this outside of context. I've edited my answer, it fits more like an comment now, but I'll let it be.
phpfiddle.org/main/code/nf6-nby The problem is this doesn't work for multiple Level 1 categories, the first set will get overwritten. I would have mentioned that before but I just realized the issue. Please the see phpfiddle for an example. Thanks guys.
I've updated the answer (links and code). It now produces this Array ( [Root] => Array ( [0] => Array ( [Root MySite.com] => Array ( [0] => Array ( [0] => Blue Widget [1] => Purple Widget ) ) ) [1] => Array ( [FooBar.com] => Array ( [0] => Array ( [0] => Blue Gizmos [1] => Purple Gizmos ) ) ) ) ).
In this example you've lost the key name 'Widgets' and 'Gizmos' .. THats why i've been messing with this for a while, can't see to get it quite right :) ... Also FooBar.com is should be the keyname of the Root Mysite[1] array , no?
|

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.