Admin/Common/function.php
function get_parent_category($cid){ if(empty($cid)){ return false; } $cates = M('Category')->where(array('status'=>1))->field('id,title,pid')->order('sort')->select(); $child = get_category($cid); //獲取引數分類的資訊 $pid = $child['pid']; $temp = array(); $res[] = $child; while(true){ foreach ($cates as $key=>$cate){ if($cate['id'] == $pid){ $pid = $cate['pid']; array_unshift($res, $cate); //將父分類插入到陣列第一個元素前 } } if($pid == 0){ break; } } return $res; }此程式碼有死迴圈,請檢查解決: 測試資料:
Array ( [0] => Array ( [id] => 1 [title] => 部落格 [pid] => 0 ) [1] => Array ( [id] => 39 [title] => test [pid] => 1 ) )