0

I've an array that has around 300 items. There are 5 properties per item. Some items have a parent item. the nested items need to be able to scale to unlimited nesting.

I need to organise the array so that all items that have a parent are within the parent item children property. The child will have children

I can create an associative array that makes the first 2 levels. The top parent items and it's children.

Now my problem comes when I am trying to create unlimited nesting. I can't see a way to automatically create a method that will adapt to multiple nesting levels and put the information to where it should be nested to.

I've never had to build such a tree and performance is crucial. Could anyone give me guidance on how could I achieve what I intend to achieve? I would really appreciate it

Edit: my issue is if i have to use recursion, not how to split what i've just done into 1 separate method.

 $tree = array();
            /*
             * $three = array( array( 'id','parent_id','displayAs', 'children' ) )
             */
            foreach ( $taxonomyFullList as $firstLevel ) {

                // Get all
                if( (int)$firstLevel['parent_id'] == 0 ) {
                    $tree[] = array( 
                                    'id' => $firstLevel['id'],
                                    'parent_id' => $firstLevel['parent_id'],
                                    'displayAs' => $firstLevel['displayAs'],
                                    'type' => $firstLevel['type'],
                                    'children' => array()
                                    );
                    $key = array_search( $firstLevel,$taxonomyFullList );
                    unset( $taxonomyFullList[$key] );
                }
            }

            foreach ( $taxonomyFullList as $secondLevel ) {
                foreach ( $tree as $firstTreeLevel ) {
                    if( (int)$secondLevel['parent_id'] === (int)$firstTreeLevel['id'] ) {

                        $newArray =  array( 
                                    'id' => $secondLevel['id'],
                                    'parent_id' => $secondLevel['parent_id'],
                                    'displayAs' => $secondLevel['displayAs'],
                                    'type' => $secondLevel['type'],
                                    'children' => array()
                                    );
                        $key = array_search( $firstTreeLevel, $tree );
                        array_push( $tree[$key]['children'], $newArray );

                        $taxonomyFullListKey = array_search( $secondLevel,$taxonomyFullList );
                        unset( $taxonomyFullList[$taxonomyFullListKey] );
                    }
                } 

            }
Jonathan Thurft
  • 4,087
  • 7
  • 47
  • 78
  • 1
    I guess that will do the stuff: http://stackoverflow.com/questions/8840319/build-a-tree-from-a-flat-array-in-php?rq=1 – Alma Do Aug 13 '13 at 14:31

1 Answers1

0

It depends on your data but you'll probably have to use recursion somewhere. First, create a function that populates one item then, when this item has children, reuse the same function.

Basically, you'll need to have something like this:

function createItem() {
    $item = array();
    // ...
    // populate the item properties
    // ...
    $item['children'] = createItem();
    return $item;
}

$root = createItem();
laurent
  • 88,262
  • 77
  • 290
  • 428
  • Yes, thats what I've so far done in my first logic (I've not separatated it on a different method but I get it). My problem is when i need to use recursion in the more nested levels – Jonathan Thurft Aug 13 '13 at 14:37
  • I don't think you get it Jonathan Thurft. This is sound advice IMO. – allen213 Aug 13 '13 at 14:39
  • @allen213 the problem is that there is not a single top level item. there are many items that will have id = 0. thus being top level. that example perse doesnt array_push to allow more than 1 item. – Jonathan Thurft Aug 13 '13 at 14:58
  • @JonathanThurft, it's easier to think of the structure as a real tree with a root. You don't actually have to show the root, but having it in your internal structure will simplify your code. In that case, the root is just like any other item, except that you don't show it. – laurent Aug 13 '13 at 15:17
  • @Laurent could you provide me with a complete working example so that I can understand it better?. as it stands the example is a bit vague (implementation wise) – Jonathan Thurft Aug 13 '13 at 15:26