當(dāng)前欄目為:dede模板 id為16
最終效果:
Array ( [0] => Array ( [category_id] => 16 [pid] => 15 [category_name] => dede模板 [category_status] => 1 [category_sort] => 99 [template_name] => index [view_template] => view [is_index] => 0 [seo_title] => [seo_key] => [seo_desc] => ) [1] => Array ( [category_id] => 15 [pid] => 10 [category_name] => wordpress模板 [category_status] => 1 [category_sort] => 99 [template_name] => index [view_template] => view [is_index] => 0 [seo_title] => [seo_key] => [seo_desc] => ) [2] => Array ( [category_id] => 10 [pid] => 6 [category_name] => 網(wǎng)站建設(shè)模板 [category_status] => 1 [category_sort] => 99 [template_name] => image [view_template] => down_view [is_index] => 0 [seo_title] => 網(wǎng)站建設(shè)模板_企業(yè)網(wǎng)站模板_Div+css模板_織夢(mèng)網(wǎng)站建設(shè)模板_wordpress模板免費(fèi)下載 - 淺笑SEO [seo_key] => 網(wǎng)站建設(shè)模板,企業(yè)網(wǎng)站模板,Div+css模板,織夢(mèng)網(wǎng)站建設(shè)模板,wordpress模板免費(fèi)下載 [seo_desc] => 新疆淺笑seo提供網(wǎng)站建設(shè)模板、企業(yè)網(wǎng)站模板、Div+css模板、織夢(mèng)網(wǎng)站建設(shè)模板、wordpress模板免費(fèi)下載,如需付費(fèi)服務(wù)請(qǐng)聯(lián)系Tel::13779140165。 ) [3] => Array ( [category_id] => 6 [pid] => 0 [category_name] => 網(wǎng)站建設(shè) [category_status] => 1 [category_sort] => 99 [template_name] => index [view_template] => view [is_index] => 1 [seo_title] => [seo_key] => [seo_desc] => ) )獲取函數(shù)代碼:
/** * 獲取當(dāng)前分類的上級(jí)分類數(shù)據(jù) * @param array $data 原有數(shù)組 * @param int $parent_id 父級(jí)id * @return array [description] */ public function getParentData(array $data,int $parent_id){ //去獲取當(dāng)前父分類數(shù)據(jù) $cache_ = self::where(['category_id'=>$parent_id,'category_status'=>1])->field('*')->find(); //如果父分類數(shù)據(jù)為空則直接返回data if (empty($cache_)) { return $data; }else{ //對(duì)象轉(zhuǎn)數(shù)組 $cache_ = json_decode(json_encode($cache_),true); //父級(jí)欄目合并到data內(nèi) $cache = array_merge($data, [$cache_]); if ($cache_['pid'] != 0) { //如果當(dāng)前父欄目上面還有父級(jí)欄目則循環(huán)獲取 $cache = self::getParentData($cache, $cache_['pid']); } //返回三維數(shù)組 return $cache; } }使用方法:
public function test(){ //實(shí)例化模型 $model = new appcommonmodelCategory; //查詢數(shù)據(jù) $data = $model->where('category_id',input('cid/d',10))->find(); //對(duì)象轉(zhuǎn)數(shù)組 $data = json_decode(json_encode($data),true); //打印數(shù)據(jù) print_r($model->getParentData([$data],$data['pid']));die; }有任何問(wèn)題歡迎在下方瀏覽或者咨詢我qq:644332569