ThinkPHP无限级分类(递归)
代码演示
没什么可说的直接看代码
<?php
namespace app\controller;
class Category
{
//模拟假数据
protected static function arr()
{
$rows = [
[
'id' => '1',
'name' => '一级菜单',
'pid' => '0',
'path' => '0',
],
[
'id' => '2',
'name' => '二级菜单',
'pid' => '0',
'path' => '0',
],
[
'id' => '3',
'name' => '一级菜单-1',
'pid' => '1',
'path' => '0-1',
],
[
'id' => '4',
'name' => '二级菜单-1',
'pid' => '2',
'path' => '0-2',
],
[
'id' => '5',
'name' => '一级菜单-1-1',
'pid' => '3',
'path' => '0-1-3',
],
[
'id' => '6',
'name' => '二级菜单-1-1',
'pid' => '4',
'path' => '0-2-4',
],
[
'id' => '7',
'name' => '二级菜单-1-2',
'pid' => '4',
'path' => '0-2-4',
],
[
'id' => '8',
'name' => '三级菜单',
'pid' => '0',
'path' => '0',
],
[
'id' => '9',
'name' => '二级菜单-1-3',
'pid' => '4',
'path' => '0-2-4-6',
],
[
'id' => '10',
'name' => '三级菜单-1',
'pid' => '8',
'path' => '0-8',
],
[
'id' => '11',
'name' => '一级菜单-1-4',
'pid' => '5',
'path' => '0-8',
],
];
return $rows;
}
public function index()
{
//获取从第0级下的所有分类(pid==0)
$list = $this->build_tree(0);
return json($list);
}
/**
* 递归子级
* @param $list
* @param $id
* @return array
*/
public function findChild($list, $id)
{
$child = [];
foreach ($list as $key => $item) {
//如果pid等于传进来
if ($item['pid'] == $id) {
$child[] = $item;
}
}
return $child;
}
/**
* 获取当前级别的子级
* @param $root_id /第几层
* @return null
*/
public function build_tree($root_id)
{
//获取假数据
$list = $this->arr();
//查找指定级数
$tree = $this->findChild($list, $root_id);
if (empty($tree)) {
return null;
}
//遍历获取到的层级得到下级分类
foreach ($tree as $key => $item) {
//递归调用自己 查找每个元素下的分类
$child = $this->build_tree($item['id']);
//如果有子类就放入数组中
if ($child != null){
$tree[$key]['child'] = $child;
}
}
return $tree;
}
}
预览结果

ThinkPHP无限级分类(递归)的更多相关文章
- C#无限级分类递归显示示例
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RoleDemo20150305 ...
- PHP无限级分类-递归(不推荐)
[http://www.helloweba.com/view-blog-204.html] 在一些复杂的系统中,要求对信息栏目进行无限级的分类,以增强系统的灵活性.那么PHP是如何实现无限级分类的呢? ...
- ThinkPHP无限级分类
<?php // +---------------------------------------------------------------------- // | ThinkPHP [ ...
- 使用Python3.7+Django2.0.4配合vue.js2.0的组件递归来实现无限级分类(递归层级结构)
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_133 所谓的无限极分类是啥?其实简单点说就是一个人类可以繁衍出多个后代,然后一个后代又可以分另外多个后代这样无限繁衍下去(可以想象 ...
- thinkphp 无限级分类的思想 分析
$list=$cate->field("id,name,pid,path,concat(path,'-',id) as bpath")->order('bpath')- ...
- PHP 无限级分类(递归)
网上有很多,这是我自己做测试用的$arr = array( array('id'=>1,'name'=>'电脑','pid'=>0), array('id'=>2,'name' ...
- thinkphp框架中使用递归实现无限级分类
无限级分类在我们开发中显得举足轻重,会经常被人问到,而一般会用递归的方法来实现,但是递归又会难倒一批人.今天博主分享的这个稍微有点基础的phper都能学会,希望大家能喜欢. 一.先建立对应的数据库和表 ...
- thinkphp中如何实现无限级分类?
thinkphp中如何实现无限级分类? 一.总结 1.数据表设计+递归算法 二.php实现无限级分类实例总结 1.数据库数据如下: 2.任务需求:给一个id,求自己和所有父亲. 3.实现代码如下:th ...
- PHP无限级分类的实现(不使用递归)
无限级分类在开发中经常使用,例如:部门结构.文章分类.无限级分类的难点在于“输出”和“查询”,例如 将文章分类输出为<ul>列表形式: 查找分类A下面所有分类包含的文章. 1.实现原理 在 ...
随机推荐
- ISODateTimeFormat 转换2019-08-15T00:36:49.366456463Z 日期格式
import java.text.*; import java.text.SimpleDateFormat; import java.util.*; import org.joda.time.Date ...
- Parentheses Balance UVA - 673
You are given a string consisting of parentheses () and []. A string of this type is said to be corr ...
- Day06_31_接口(Interface)
java 接口(Interface) 接口和抽象类的区别? 子类只能通过extends关键字去继承抽象类(Abstract),子类(如果不是抽象类)则必须覆写抽象类之中的全部抽象方法(如果子类没有实现 ...
- ESP32音频开发板ESP32-Korvo V1.1踩坑
电池供电ESP32-Korvo V1.1开发板供电电压低于3.9V不断复位: 报错->Brownout detector was triggered 断电探测器触发复位 根据同行资料发现,禁用断 ...
- Java 基础 一文搞懂泛型
本文将从以下四个方面来系统的讲解一下泛型,基本上涵盖了泛型的主体内容. 什么是泛型? 为什么要使用泛型? 如何使用泛型? 泛型的特性 1. 什么是泛型? 泛型的英文是Generics,是指在定义方法. ...
- 2021S软件工程——结对项目第三阶段
2021S软件工程--结对项目第三阶段 2021春季软件工程(罗杰 任健) 项目地址 1020 1169 1 实践反思 1.1 问题分析 两人习惯不一致 没有具体制定时间节点 写完代码才开始" ...
- 1019 General Palindromic Number
A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...
- 【golang】golang中结构体的初始化方法(new方法)
准备工作: 定义结构体:Student import ( "fmt" "reflect") type Student struct { StudentId st ...
- Windows系统之间文件互传
1)利用Windows自带的文件共享服务 本次试验以Win7为服务器端,win10为客户端 1.确保Win7服务端开启对应的服务及开放相应的端口号 进入命令行界面,输入netstat -an,查看44 ...
- POJ2752KMP逆序处理
题意: 给你一个串,问你都哪些位置即使前缀又是后缀. 思路: 可以用KMP来做,尝试过很多种方法,想把两个串接起来然后..失败,后来又想可以倒着匹配,就是把整个串倒过来..失败,说 ...