bonez
bonez

Reputation: 695

Having trouble with a recursion algo

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...

Upvotes: 0

Views: 95

Answers (1)

atomman
atomman

Reputation: 2507

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

Upvotes: 2

Related Questions