How to create a tree that does not have a single vertex using associative arrays

advertisements

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] );
                    }
                } 

            }


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();