rulururu

post Converting a flat array with Parent ID’s to a nested Tree

March 3rd, 2009

Filed under: Code, PHP — Brenton Alker @ 11:37

Storing hierarchical data in a tabular data structure such as a database is not uncommon in many applications (eg. threaded comments on a blog entry, or a navigation menu structure). The “Adjacency List” method; probably the most common, involves storing a reference to the parent in each of the children.

Here is a snippet for converting the rows stored using the adjacency list method into a hierarchical array in PHP. My goal was to do this in 1 pass, without recursion. I ended up using 2 passes, I think 1 pass is achievable if you can guarantee the parent will always be returned before all of its children. But since I couldn’t, the first pass is to change the id of the row to be the key of the array.

function convertToTree(array $flat, $idField = 'id',
                        $parentIdField = 'parentId',
                        $childNodesField = 'childNodes') {
    $indexed = array();
    // first pass - get the array indexed by the primary id
    foreach ($flat as $row) {
        $indexed[$row[$idField]] = $row;
        $indexed[$row[$idField]][$childNodesField] = array();
    }  

    //second pass
    $root = null;
    foreach ($indexed as $id => $row) {
        $indexed[$row[$parentIdField]][$childNodesField][$id] =& $indexed[$id];
        if (!$row[$parentIdField]) {
            $root = $id;
        }
    }

    return array($root => $indexed[$root]);
}  

// Usage:
$rows = array(
    array(
        'id' => 1,
        'parentId' => null,
        'name' => 'Menu',
    ),
    array(
        'id' => 2,
        'parentId' => 1,
        'name' => 'Item 1-1',
    ),
    array(
        'id' => 3,
        'parentId' => 1,
        'name' => 'Item 2-1',
    ),
    array(
        'id' => 4,
        'parentId' => 1,
        'name' => 'Item 1-2',
    ),
);

convertToTree($rows);

// Produces:
array (
    1 => array(
        'id' => 1,
        'parentId' => NULL,
        'name' => 'Menu',
        'childNodes' => array(
            2 => array(
                'id' => 2,
                'parentId' => 1,
                'name' => 'Item 1-1',
                'childNodes' => array (
                    4 => array (
                        'id' => 4,
                        'parentId' => 2,
                        'name' => 'Item 1-2',
                        'childNodes' => array (
                        ),
                    ),
                ),
            ),
            3 =>  array (
                'id' => 3,
                'parentId' => 1,
                'name' => 'Item 2-1',
                'childNodes' => array (
                ),
            ),
        ),
    ),
)

7 Comments »

  1. hi,

    may there is mistake, because item id = 4 has parentId = 1 not 3. Second problem is in return value of function. Index 0 isn’t in array.

    Comment by Pavel Jirák — 2009-04-30 @ 22:16

  2. Right you are. That’s what I get for “fixing” code without testing it.

    Updated post.

    Comment by Brenton Alker — 2009-04-30 @ 22:47

  3. Hey, sweet function :)

    I had one small problem though, my root node had ‘parentId’ = ‘0′, so when checking if ($row[$parentIdField] === null) it never finds the root node.

    better solution may be like this
    if (empty($row[$parentIdField]))

    I know that === null is faster, but this way was more convenient for me.

    Thanks a lot ;)

    Comment by Sasa Tomislav Mataic — 2009-05-06 @ 20:49

  4. Good point, I can’t see any reason to look for NULL specifically. I’ve gone with a check for boolean false equivalents instead (mainly because empty was causing odd rendering issues and I’m too lazy to figure out why).

    Updated again.

    Comment by Brenton Alker — 2009-05-07 @ 16:51

  5. Hey, function works like a charm, but only if you have only 1 master item. If for example 2 items have parent = null, it returns only last branch of tree. I’ve achieved good results with:
    //second pass
    $root = null;
    foreach ($indexed as $id => $row) {
    $indexed[$row[$parentIdField]][$childNodesField][$id] =& $indexed[$id];
    if ($row[$parentIdField] === null) {
    $root = $id;
    $roots[] = $id;
    }
    }

    //Had to be modified, beacuse it returned only the last indexed root id
    foreach ($roots as $k) {
    $return[$k] = $indexed[$k];
    }
    return $return;

    Comment by misz — 2009-09-15 @ 20:32

  6. Wow. blog.tekerson.com is amazing.

    http://inhumanlyvideo.blogspot.com/2010/03/wattmeter-video.html

    Comment by Paige — 2010-03-14 @ 01:19

  7. blog.tekerson.com, how do you do it?

    http://balkanise670am.blogspot.com/

    Comment by Scotty — 2010-03-14 @ 12:28

RSS feed for comments on this post. TrackBack URI

Leave a comment

ruldrurd
Powered by WordPress, Web Design by Laurentiu Piron Monitored by SiteUpTime
Entries (RSS) and Comments (RSS)