日期:2014-05-17 浏览次数:20397 次
//SELECT id,pid,path FROM calss;取得数据集组合成下面的数组 $array = array('1' => 0, '2' => 1, '3' => 2, '4' => 2, '5' => 3, '6' => 3, '7' => 3, '8' => 0, '9' => 8); $pathArray = array(1 => 'web', 2 => 'control', 3 => 'language', 4 => 'data', 5 => 'php', 6 => 'jsp', 7 => 'asp', 8 => 'phone', 9 => 'ios'); //取得id列表 $idArry=array(); getTopID(5, $array); //循环id列表输出最后结果 echo listPath(5, $idArr, $pathArray), "\n"; unset($idArr); getTopID(6, $array); echo listpath(6, $idArr, $pathArray), "\n"; unset($idArr); getTopID(8, $array); echo listpath(8, $idArr, $pathArray), "\n"; unset($idArr); getTopID(9, $array); echo listpath(9, $idArr, $pathArray), "\n"; unset($idArr); //函数 /** * 取得上级Id数组 * @param $id * @param $array * @return array */ function getTopID($id, $array) { global $idArr; if (isset($array[$id])) { $idArr[] = $array[$id]; getTopID($array[$id], $array); } } /** * 根据上级ID数组排序结果 * @param $id * @param $list * @param $array * @return string */ function listPath($id, $list, $array) { $path = ''; array_push($list, $id); sort($list); foreach ($list as $value) { if (isset($array[$value])) { $path .= $array[$value] . '/'; } } return $path; }