婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

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

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

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

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

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

這時(shí)候在處理展示過程就需要將上面的結(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)
 {
  // 初始化頂級(jí)父節(jié)點(diǎn)
  if (! count($treeList)) {
   foreach($menuList as $index => $menu) {
    if ($menu['parent_id'] == 0) {
     $treeList[] = $menu;
     unset($menuList[$index]);
    }
   }
  }

  // 遞歸查找子節(jié)點(diǎn)
  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 {
    // 遞歸臨界點(diǎn)
    return false;
   }
  }
 }

}

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

happy coding!

每一個(gè)不曾起舞的日子,都是對(duì)生命的辜負(fù) ^-^

總結(jié)

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

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

標(biāo)簽:海北 濮陽(yáng) 惠州 梅河口 文山 湘西 廈門 黔東

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php菜單/評(píng)論數(shù)據(jù)遞歸分級(jí)算法的實(shí)現(xiàn)方法》,本文關(guān)鍵詞  php,菜單,評(píng)論,數(shù)據(jù),遞歸,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《php菜單/評(píng)論數(shù)據(jù)遞歸分級(jí)算法的實(shí)現(xiàn)方法》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于php菜單/評(píng)論數(shù)據(jù)遞歸分級(jí)算法的實(shí)現(xiàn)方法的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 巧家县| 方山县| 澄城县| 漳浦县| 岳阳市| 宾川县| 万源市| 汝城县| 大丰市| 贺州市| 东兰县| 珠海市| 松桃| 峨边| 阿拉善右旗| 余江县| 通州区| 麟游县| 海门市| 吴江市| 宝兴县| 汉川市| 兴海县| 邢台县| 通化县| 依安县| 桂林市| 京山县| 延津县| 昭通市| 理塘县| 郴州市| 三门峡市| 突泉县| 柳林县| 郁南县| 勃利县| 英吉沙县| 汝南县| 慈利县| 临沂市|