Array "tree" creation from db table

Posted by Tural Teyyuboglu on Stack Overflow See other posts from Stack Overflow or by Tural Teyyuboglu
Published on 2011-11-22T01:30:16Z Indexed on 2011/11/22 1:51 UTC
Read the original article Hit count: 172

Filed under:
|
|
|

Trying to create array tree for db driven navigation. Getting following errur:

array_key_exists() expects exactly 2 parameters, 1 given on line if (!array_key_exists($tree[$parent]['children'][$id]))

Function looks like that

$tree = array();
$sql = "SELECT id, parent, name FROM menu WHERE parent ... etc.... ";
$results = mysql_query($sql) or die(mysql_error());
while(list($id, $parent, $name) = mysql_fetch_assoc($results)) {
    $tree[$id] = array('name' => $name, 'children' => array(), 'parent' => $parent);
    if (!array_key_exists($tree[$parent]['children'][$id])) {
        $tree[$parent]['children'][$id] = $id;
    }
}

Db structure

http://img338.imageshack.us/img338/4669/6df2a8e6c7684c8aaa82ddf.png

How can I fix that?

Whats wrong in this function?

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql