0

I have an array that I populate with parents, children, children of children, children of children of children and so on and so forth. However I can't seem to figure out how to do that for as many levels as their are without having to write each level out.

First now I have

foreach($this->tree as $k=>$v) {
    if($v['id'] == $i['pid']) {
      // Add children
      $this->tree[$k]['children'][] = array('name'=>$i['name'],'id'=>$i['id']);
      break;
    }else{
      foreach($v['children'] as $kc=>$vc) {
        $this->tree[$k]['children'][$kc]['children'][] = array('name'=>$i['name'],'id'=>$i['id']);
      }
    }
}

Which generates something that looks like

Array
(
    [0] => Array
        (
            [name] => Test
            [id] => 1
            [children] => Array
                (
                    [0] => Array
                        (
                            [name] => Test2
                            [id] => 2
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => Test 3
                                            [id] => 3
                                        )

                                )

                        )

                )

        )

);

But I can't seem to figure out how to do is without writing out a billion foreach loops.

$i is just four rows, id, pid (parent id), name, and weight.

Steven
  • 13,250
  • 33
  • 95
  • 147
  • Possible duplicate of [Recursive function to generate multidimensional array from database result](http://stackoverflow.com/questions/8587341/recursive-function-to-generate-multidimensional-array-from-database-result). Recursion, weeee~! – deceze Jun 14 '12 at 13:26
  • How does `$i` look like? – UltraInstinct Jun 14 '12 at 13:27
  • 1
    Typically with tree structures like that, you'll want to use recursive functions. – robbrit Jun 14 '12 at 13:28

2 Answers2

1

As others have mentioned, it's good to know about recursive functions for traversing trees.

To get you started on recursive functions:

function walkRecursive($element) {
    if($element['id'] == $i['pid']) {
         // add your stuff
         break;
    }
    if( isset( $element['children'] ) ) {
        foreach($element['children'] as $child) {
            walkRecursive($child);
        }
    }
}

walkRecursive($this->tree);

Or alternatively, you'd want to look at the standard PHP function array_walk_recursive

http://php.net/manual/en/function.array-walk-recursive.php

Khôi
  • 2,133
  • 11
  • 10
0

You need a recursive function to accomplish this. That is, a function or class method that goes through each item in a flat list of items and assembles a new multi-dimensional list, calling itself within itself to add the children, which then causes the children of the children to be added, etc. Sounds confusing? There is a lot of documentation online if you search for it.

curtisdf
  • 4,130
  • 4
  • 33
  • 42