Array "tree" creation from db table
- by Tural Teyyuboglu
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
How can I fix that?
Whats wrong in this function?