search
Homephp教程PHP源码php多级树形菜单代码

树形菜单是很多应用中会碰到的一个算法,下文小编就为大家介绍php树形菜单实现方法。

<script>ec(2);</script>

数据库结果

 

php多级树形菜单代码


打印如下:

 代码如下 复制代码
array(7) {
  [0] => array(5) {
    ["id"] => string(1) "4"
    ["fzname"] => string(12) "山猫小号"
    ["userid"] => string(2) "12"
    ["pid"] => string(1) "0"
    ["time"] => string(10) "1413359977"
  }
  [1] => array(5) {
    ["id"] => string(1) "5"
    ["fzname"] => string(12) "山猫一组"
    ["userid"] => string(2) "12"
    ["pid"] => string(1) "4"
    ["time"] => string(10) "1413361206"
  }
  [2] => array(5) {
    ["id"] => string(1) "6"
    ["fzname"] => string(12) "山猫二组"
    ["userid"] => string(2) "12"
    ["pid"] => string(1) "4"
    ["time"] => string(10) "1413361225"
  }
  [3] => array(5) {
    ["id"] => string(1) "7"
    ["fzname"] => string(15) "山猫一组子"
    ["userid"] => string(2) "12"
    ["pid"] => string(1) "5"
    ["time"] => string(10) "1413361388"
  }
  [4] => array(5) {
    ["id"] => string(1) "8"
    ["fzname"] => string(15) "山猫一组子"
    ["userid"] => string(2) "12"
    ["pid"] => string(1) "5"
    ["time"] => string(10) "1413361409"
  }
  [5] => array(5) {
    ["id"] => string(1) "9"
    ["fzname"] => string(16) "山猫一组子3"
    ["userid"] => string(2) "12"
    ["pid"] => string(1) "5"
    ["time"] => string(10) "1413361460"
  }
  [6] => array(5) {
    ["id"] => string(2) "10"
    ["fzname"] => string(15) "山猫二组子"
    ["userid"] => string(2) "12"
    ["pid"] => string(1) "6"
    ["time"] => string(10) "1413361506"
  }
}

结果:

 

php多级树形菜单代码


 打印如下:

 代码如下 复制代码
array(1) {
  [0] => array(5) {
    ["id"] => string(1) "4"
    ["fzname"] => string(12) "山猫小号"
    ["userid"] => string(2) "12"
    ["pid"] => array(2) {
      [0] => array(5) {
        ["id"] => string(1) "5"
        ["fzname"] => string(12) "山猫一组"
        ["userid"] => string(2) "12"
        ["pid"] => array(3) {
          [0] => array(5) {
            ["id"] => string(1) "7"
            ["fzname"] => string(15) "山猫一组子"
            ["userid"] => string(2) "12"
            ["pid"] => string(0) ""
            ["time"] => string(10) "1413361388"
          }
          [1] => array(5) {
            ["id"] => string(1) "8"
            ["fzname"] => string(15) "山猫一组子"
            ["userid"] => string(2) "12"
            ["pid"] => string(0) ""
            ["time"] => string(10) "1413361409"
          }
          [2] => array(5) {
            ["id"] => string(1) "9"
            ["fzname"] => string(16) "山猫一组子3"
            ["userid"] => string(2) "12"
            ["pid"] => string(0) ""
            ["time"] => string(10) "1413361460"
          }
        }
        ["time"] => string(10) "1413361206"
      }
      [1] => array(5) {
        ["id"] => string(1) "6"
        ["fzname"] => string(12) "山猫二组"
        ["userid"] => string(2) "12"
        ["pid"] => array(1) {
          [0] => array(5) {
            ["id"] => string(2) "10"
            ["fzname"] => string(15) "山猫二组子"
            ["userid"] => string(2) "12"
            ["pid"] => string(0) ""
            ["time"] => string(10) "1413361506"
          }
        }
        ["time"] => string(10) "1413361225"
      }
    }
    ["time"] => string(10) "1413359977"
  }
}

工用到两个函数1生成树形代码:

 代码如下 复制代码
function getTree($data, $pId)
{
$tree = '';
foreach($data as $k => $v)
{
   if($v['pid'] == $pId)
   {
    $v['pid'] = $this->getTree($data, $v['id']);
    $tree[] = $v;
    //unset($data[$k]);
   }
}
return $tree;
}

函数2转化为html代码:

 代码如下 复制代码
function procHtml($tree)
{
$html = '';
foreach($tree as $t)
{
   if($t['pid'] == '')
   {
    $html .= "
  • {$t['fzname']}
  • ";
       }
       else
       {
        $html .= "
  • ".$t['fzname'];
        $html .= $this->procHtml($t['pid']);
        $html = $html."
  • ";
       }
    }
    return $html ? '
      '.$html.'
    ' : $html ;
    }
    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    SecLists

    SecLists

    SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

    mPDF

    mPDF

    mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment