濮阳杆衣贸易有限公司

主頁 > 知識庫 > php菜單/評論數(shù)據(jù)遞歸分級算法的實現(xiàn)方法

php菜單/評論數(shù)據(jù)遞歸分級算法的實現(xiàn)方法

熱門標簽:廣州防封卡外呼系統(tǒng)多少錢一個月 怎么向銷售公司推銷外呼系統(tǒng) 哪里辦理400電話 外呼系統(tǒng)撥打暫時無法接通 高德地圖標注家 江西手機自動外呼防封系統(tǒng)是什么 仁和怎么申請400開頭的電話 長春人工外呼系統(tǒng)服務(wù)商 廣東地市地圖標注

在開發(fā)過程中經(jīng)常會遇到分級場景,如菜單分級、評論、商品類型分級等;在同一張mysql數(shù)據(jù)表中可能設(shè)計單表結(jié)構(gòu),如同如下數(shù)據(jù):

 $menuList = [
  [ 'id' => 1,'parent_id' => 0, 'name' => '節(jié)點1'],
  [ 'id' => 2,'parent_id' => 1, 'name' => '節(jié)點1-1'],
  [ 'id' => 3,'parent_id' => 0, 'name' => '節(jié)點2'],
  [ 'id' => 4,'parent_id' => 3, 'name' => '節(jié)點2-1'],
  [ 'id' => 5,'parent_id' => 2, 'name' => '節(jié)點1-1-1'],
  [ 'id' => 6,'parent_id' => 1, 'name' => '節(jié)點1-2'],
 ];

這時候在處理展示過程就需要將上面的結(jié)構(gòu)轉(zhuǎn)換為更加直觀的數(shù)據(jù)結(jié)構(gòu), 形如:

$treeList = [
 [
 children: [
  children: []
 ]
 ]
 [,
 children: [
  children: []
 ]
 ]
];

算法代碼如下:

?php

class Menu
{
 /**
  * 遞歸循環(huán)菜單列表, 轉(zhuǎn)化為菜單樹
  * @param $treeList 菜單樹列表
  * @param $menuList 菜單列表
  * @return bool
  */
 public function getMenuTree($treeList, $menuList)
 {
  // 初始化頂級父節(jié)點
  if (! count($treeList)) {
   foreach($menuList as $index => $menu) {
    if ($menu['parent_id'] == 0) {
     $treeList[] = $menu;
     unset($menuList[$index]);
    }
   }
  }

  // 遞歸查找子節(jié)點
  foreach ($treeList as $tree) {
   foreach ($menuList as $index => $menu) {
    if (empty($tree['children'])) {
     $tree['children'] = [];
    }
    if ($menu['parent_id'] == $tree['id']) {
     $tree['children'][] = $menu;
     unset($menuList[$index]);
    }
   }
   if (! empty($tree['children'])) {
    $this->getMenuTree($tree['children'], $menuList);
   } else {
    // 遞歸臨界點
    return false;
   }
  }
 }

}

$menuList = [
 [ 'id' => 1,'parent_id' => 0, 'name' => '節(jié)點1'],
 [ 'id' => 2,'parent_id' => 1, 'name' => '節(jié)點1-1'],
 [ 'id' => 3,'parent_id' => 0, 'name' => '節(jié)點2'],
 [ 'id' => 4,'parent_id' => 3, 'name' => '節(jié)點2-1'],
 [ 'id' => 5,'parent_id' => 2, 'name' => '節(jié)點1-1-1'],
 [ 'id' => 6,'parent_id' => 1, 'name' => '節(jié)點1-2'],
];
$treeList = [];
(new Menu)->getMenuTree($treeList, $menuList);
print_r($treeList);

happy coding!

每一個不曾起舞的日子,都是對生命的辜負 ^-^

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。

您可能感興趣的文章:
  • php求斐波那契數(shù)的兩種實現(xiàn)方式【遞歸與遞推】
  • PHP實現(xiàn)無限極分類的兩種方式示例【遞歸和引用方式】
  • PHP利用遞歸函數(shù)實現(xiàn)無限級分類的方法
  • PHP自定義遞歸函數(shù)實現(xiàn)數(shù)組轉(zhuǎn)JSON功能【支持GBK編碼】
  • PHP遞歸寫入MySQL實現(xiàn)無限級分類數(shù)據(jù)操作示例
  • PHP數(shù)組遞歸排序?qū)崿F(xiàn)方法示例
  • PHP迭代與遞歸實現(xiàn)無限級分類
  • PHP實現(xiàn)遞歸的三種方法

標簽:海北 濮陽 惠州 梅河口 文山 湘西 廈門 黔東

巨人網(wǎng)絡(luò)通訊聲明:本文標題《php菜單/評論數(shù)據(jù)遞歸分級算法的實現(xiàn)方法》,本文關(guān)鍵詞  php,菜單,評論,數(shù)據(jù),遞歸,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《php菜單/評論數(shù)據(jù)遞歸分級算法的實現(xiàn)方法》相關(guān)的同類信息!
  • 本頁收集關(guān)于php菜單/評論數(shù)據(jù)遞歸分級算法的實現(xiàn)方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    阳高县| 通河县| 阳朔县| 乌兰县| 聂拉木县| 南开区| 五家渠市| 历史| 拉萨市| 松原市| 庆阳市| 古蔺县| 木兰县| 康乐县| 靖安县| 怀仁县| 会泽县| 满洲里市| 漳平市| 庄浪县| 潜山县| 贵南县| 邳州市| 万安县| 德江县| 疏勒县| 墨脱县| 汉沽区| 沙田区| 青海省| 喜德县| 玉溪市| 临武县| 桓台县| 甘谷县| 上杭县| 泸定县| 大安市| 泰和县| 剑阁县| 绿春县|