PHP完全自学手册(珍藏版) 中文pdf扫描版下载
$arr = array( array('id' => 1, 'parent' => 0), array('id' => 2, 'parent' => 0), array('id' => 3, 'parent' => 0), array('id' => 4, 'parent' => 1), array('id' => 5, 'parent' => 4), array('id' => 6, 'parent' => 5)); function get_key($arr, $id) { foreach ($arr as $key => $val) { if ($val['id'] === $id) { return $key; } } return null; } function get_parent($arr, $id) { $key = get_key($arr, $id); if ($arr[$key]['parent'] == 0) { return $id; } else { return get_parent($arr, $arr[$key]['parent']); } } echo get_parent($arr, 6);
转载请注明:谷谷点程序 » php无限分类的简单实现方法