日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

无限级分类及生成json数据

發(fā)布時間:2025/3/15 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 无限级分类及生成json数据 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

第一步,先去數(shù)據(jù)庫查詢類別數(shù)據(jù),然后交給生成json數(shù)據(jù)的函數(shù)處理,代碼如下:

1 /*生成類別JSON數(shù)據(jù)*/ 2 public function wirteJson(){ 3 $dataInfo = \think\Db::query("select id as v,name as n,pid from think_pro_category"); 4 $data = $this->getCategoryJson($dataInfo); 5 return $data; 6 }

第二步,將查詢出來的類別數(shù)據(jù)重新歸類排序,代碼如下:

1 /** 2 *處理分類數(shù)組 3 **/ 4 function generateTree($items) { 5 $tree = array(); 6 foreach($items as $item){ 7 if(isset($items[$item['pid']])){ 8 $items[$item['pid']]['s'][] = &$items[$item['v']]; 9 }else{ 10 $tree[] = &$items[$item['v']]; 11 } 12 } 13 return $tree; 14 }

第三步,生成json數(shù)據(jù),并返回數(shù)據(jù),代碼如下:

1 /** 2 * 功能:無限級類別json數(shù)據(jù)生成 3 * 參數(shù):$data 類別查詢結(jié)果集 4 * 返回值:$json 遞歸查詢排序后的json數(shù)據(jù) 5 */ 6 public function getCategoryJson($dataInfo) { 7 /*生成json數(shù)據(jù)*/ 8 foreach($dataInfo as $category) { 9 $tree[$category['v']] = $category; 10 $tree[$category['v']]['s'] = array(); 11 } 12 $content = json_encode(generateTree($tree)); 13 $content = str_replace(',"s":[]', "", $content); 14 // for( $i = 0; $i < count($dataInfo); $i++ ) { 15 // $content = str_replace('"'.$dataInfo[$i]['v'].'":', "", $content); 16 // } 17 //$content = '['.substr($content,1,strlen($content)-2).']'; 18 //return $content; 19 /*寫入文件*/ 20 //文件存放路徑 21 $filePath = $_SERVER['DOCUMENT_ROOT'].DS.'/category/category.json'; 22 $returnval = file_put_contents($filePath,$content); 23 // $fopen = fopen($filePath,'w+'); 24 // fwrite($fopen,$content); 25 // fclose($fopen); 26 return $returnval; 27 }

轉(zhuǎn)載于:https://www.cnblogs.com/walblog/p/8005333.html

總結(jié)

以上是生活随笔為你收集整理的无限级分类及生成json数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。