平时开发中或多或少不可避免会遇到无限极分类的问题,因为效率、逻辑等问题也一直使这类问题比较尖锐。今天我们以yii2框架为基础,栏目无限极为例,对这个问题进行一个简单的处理。
首先我们有一张栏目数据表 tree
表结构如下图(原文有图)
看上去表结构很简单。
我们插入几条测试数据
INSERT INTO `tree` (`id`, `parent_id`, `name`) VALUES (1, 0, 'A'), (2, 0, 'B'), (3, 1, 'a'), (4, 3, 'aa'), (5, 2, 'b'), (6, 4, 'aaa');
树形结构大致如下
|A |--a |----aa |------aaa |B |--b
这也正是我们所需要的数据结构形式,下面我们来看看如何处理才能够得到所需要的结果。
我们前面也说了,以yii2为基础,因此我们的写法也按照面向对象的规则来
class tree { //访问index查看树形结构 public function actionIndex () { $data = self::getTree(); //为了方便测试,我们这里以json格式输出 \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; return $data; } //获取树 public static function getTree () { //这里我们直接获取所有的数据,然后通过程序进行处理 //在无限极分类中最忌讳的是对数据库进行层层操作,也就很容易造成内存溢出 //最后电脑死机的结果 $data = static::find()->all(); return self::_generateTree($data); } //生成树 private static function _generateTree ($data, $pid = 0) { $tree = []; if ($data && is_array($data)) { foreach($data as $v) { if($v['parent_id'] == $pid) { $tree[] = [ 'id' => $v['id'], 'name' => $v['name'], 'parent_id' => $v['parent_id'], 'children' => self::_generateTree($data, $v['id']), ]; } } } return $tree; } }
我们访问下tree/index看看,效果图如下
这样我们可以看到一个很清晰的树形结构图,也就是我们最终所需要的。

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:

Laravel's service container and service providers are fundamental to its architecture. This article explores service containers, details service provider creation, registration, and demonstrates practical usage with examples. We'll begin with an ove


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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),

Notepad++7.3.1
Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
