Search code examples
phparraysmultidimensional-arrayarray-key-exists

Array key exists in multidimensional array


I'm trying to arrange a group of pages in to an array and place them depending on their parent id number. If the parent id is 0 I would like it to be placed in the array as an array like so...

$get_pages = 'DATABASE QUERY'
$sorted = array()

foreach($get_pages as $k => $obj) {
    if(!$obj->parent_id) {
        $sorted[$obj->parent_id] = array();
    }
}

But if the parent id is set I'd like to place it in to the relevant array, again as an array like so...

$get_pages = 'DATABASE QUERY'
$sorted = array()

foreach($get_pages as $k => $obj) {
    if(!$obj->parent_id) {
        $sorted[$obj->id] = array();
    } else if($obj->parent_id) {
        $sorted[$obj->parent_id][$obj->id] = array();
    }
}

This is where I begin to have a problem. If I have a 3rd element that needs to be inserted to the 2nd dimension of an array, or even a 4th element that needs inserting in the 3rd dimension I have no way of checking if that array key exists. So what I can't figure out is how to detect if an array key exists after the 1st dimension and if it does where it is so I can place the new element.

Here is an example of my Database Table

id    page_name    parent_id

1     Products             0
2     Chairs               1
3     Tables               1
4     Green Chairs         2
5     Large Green Chair    4
6     About Us             0

Here is an example of the output I'd like to get, if there is a better way to do this I'm open for suggestions.

Array([1]=>Array([2] => Array([4] => Array([5] => Array())), [3] => Array()), 6 => Array())

Thanks in advanced!


Solution

  • Well, essentially you are building a tree so one of the ways to go is with recursion:

    // This function takes an array for a certain level and inserts all of the 
    // child nodes into it (then going to build each child node as a parent for
    // its respective children):
    
    function addChildren( &$get_pages, &$parentArr, $parentId = 0 )
    {
        foreach ( $get_pages as $page )
        {
            // Is the current node a child of the parent we are currently populating?
    
            if ( $page->parent_id == $parentId )
            {
                // Is there an array for the current parent?
    
                if ( !isset( $parentArr[ $page->id ] ) )
                {
                    // Nop, create one so the current parent's children can
                    // be inserted into it.
    
                    $parentArr[ $page->id ] = array();
                }
    
                // Call the function from within itself to populate the next level
                // in the array:
    
                addChildren( $get_pages, $parentArr[ $page->id ], $page->id );
            }
        }
    }
    
    
    $result = array();
    addChildren( $get_pages, $result );
    
    print_r($result);
    

    This is not the most efficient way to go but for a small number of pages & hierarchies you should be fine.