PHP实现无限极归类
PHP实现无限极分类
利用两个for循环实现无限级分类
表:
字段名 | 字段类型 | 备注 | 默认值 |
---|---|---|---|
id | int | 主键 auto-increment | |
name | varchar | 分类名称 | |
pid | int | 父类id | 0 |
顶级分类的 pid 默认就是0了。当我们想取出某个分类的子分类树的时候,基本思路就是递归,当然,出于效率问题不建议每次递归都查询数据库,通常的做法是先将所有分类取出来,数据保存到PHP数组里,再进行处理,最后还可以将结果缓存起来以提高下次请求的效率。
先来构建一个原始数组,这个直接从数据库中查询出来就行:
1. 构建数据
<code class=" hljs php"><span class="hljs-variable">$categories</span> = <span class="hljs-keyword">array</span>( <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-number">1</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'电脑'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-number">0</span>), <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-number">2</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'手机'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-number">0</span>), <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-number">3</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'笔记本'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-number">1</span>), <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-number">4</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'台式机'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-number">1</span>), <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-number">5</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'智能机'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-number">2</span>), <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-number">6</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'功能机'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-number">2</span>), <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-number">7</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'超级本'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-number">3</span>), <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-number">8</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'游戏本'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-number">3</span>),);</code>
目标是将它转化为下面这种结构
电脑 >笔记本 >>超级本 >> 游戏本 > 台式机
手机 > 智能机 > 功能机
用数组来表示的话,可以增加一个 children 键来存储它的子分类:
<code class=" hljs php"><span class="hljs-keyword">array</span>( <span class="hljs-comment">//1对应$categories中的id ,方便直接读取</span> <span class="hljs-number">1</span> => <span class="hljs-keyword">array</span>( <span class="hljs-string">'id'</span>=><span class="hljs-number">1</span>, <span class="hljs-string">'name'</span>=><span class="hljs-string">'电脑'</span>, <span class="hljs-string">'pid'</span>=><span class="hljs-number">0</span>, children=><span class="hljs-keyword">array</span>( &<span class="hljs-keyword">array</span>( <span class="hljs-string">'id'</span>=><span class="hljs-number">3</span>, <span class="hljs-string">'name'</span>=><span class="hljs-string">'笔记本'</span>, <span class="hljs-string">'pid'</span>=><span class="hljs-number">1</span>, <span class="hljs-string">'children'</span>=><span class="hljs-keyword">array</span>( <span class="hljs-comment">//此处省略</span> ) ), &<span class="hljs-keyword">array</span>( <span class="hljs-string">'id'</span>=><span class="hljs-number">4</span>, <span class="hljs-string">'name'</span>=><span class="hljs-string">'台式机'</span>, <span class="hljs-string">'pid'</span>=><span class="hljs-number">1</span>, <span class="hljs-string">'children'</span>=><span class="hljs-keyword">array</span>( <span class="hljs-comment">//此处省略</span> ) ), ) ), <span class="hljs-comment">//其他分类省略</span>)</code>
2. 处理过程:
<code class=" hljs php"><span class="hljs-variable">$tree</span> = <span class="hljs-keyword">array</span>();<span class="hljs-comment">//第一步,将所有的分类id作为数组key,并创建children单元</span><span class="hljs-keyword">foreach</span>(<span class="hljs-variable">$categories</span> <span class="hljs-keyword">as</span> <span class="hljs-variable">$category</span>){ <span class="hljs-variable">$tree</span>[<span class="hljs-variable">$category</span>[<span class="hljs-string">'id'</span>]] = <span class="hljs-variable">$category</span>; <span class="hljs-variable">$tree</span>[<span class="hljs-variable">$category</span>[<span class="hljs-string">'id'</span>]][<span class="hljs-string">'children'</span>] = <span class="hljs-keyword">array</span>();}<span class="hljs-comment">//第二步,利用引用,将每个分类添加到父类children数组中,这样一次遍历即可形成树形结构。</span><span class="hljs-keyword">foreach</span> (<span class="hljs-variable">$tree</span> <span class="hljs-keyword">as</span> <span class="hljs-variable">$key</span>=><span class="hljs-variable">$value</span>) { <span class="hljs-keyword">if</span> (<span class="hljs-variable">$value</span>[<span class="hljs-string">'pid'</span>] != <span class="hljs-number">0</span>) { <span class="hljs-variable">$tree</span>[<span class="hljs-variable">$value</span>[<span class="hljs-string">'pid'</span>]][<span class="hljs-string">'children'</span>][] = &<span class="hljs-variable">$tree</span>[<span class="hljs-variable">$key</span>]; }}print_r(<span class="hljs-variable">$tree</span>);</code>
<code>注:必须通过引用, 否则不会一次遍历就生成最终的结果.</code>
3. 打印结果如下:
<code class=" hljs php"><span class="hljs-keyword">Array</span>( [<span class="hljs-number">1</span>] => <span class="hljs-keyword">Array</span> ( [id] => <span class="hljs-number">1</span> [name] => 电脑 [pid] => <span class="hljs-number">0</span> [children] => <span class="hljs-keyword">Array</span> ( [<span class="hljs-number">0</span>] => <span class="hljs-keyword">Array</span> ( [id] => <span class="hljs-number">3</span> [name] => 笔记本 [pid] => <span class="hljs-number">1</span> [children] => <span class="hljs-keyword">Array</span> ( [<span class="hljs-number">0</span>] => <span class="hljs-keyword">Array</span> ( [id] => <span class="hljs-number">7</span> [name] => 超级本 [pid] => <span class="hljs-number">3</span> [children] => <span class="hljs-keyword">Array</span> ( ) ) [<span class="hljs-number">1</span>] => <span class="hljs-keyword">Array</span> ( [id] => <span class="hljs-number">8</span> [name] => 游戏本 [pid] => <span class="hljs-number">3</span> [children] => <span class="hljs-keyword">Array</span> ( ) ) ) ) [<span class="hljs-number">1</span>] => <span class="hljs-keyword">Array</span> ( [id] => <span class="hljs-number">4</span> [name] => 台式机 [pid] => <span class="hljs-number">1</span> [children] => <span class="hljs-keyword">Array</span> ( ) ) ) ) [<span class="hljs-number">2</span>] => <span class="hljs-keyword">Array</span> ( [id] => <span class="hljs-number">2</span> [name] => 手机 [pid] => <span class="hljs-number">0</span> [children] => <span class="hljs-keyword">Array</span> ( [<span class="hljs-number">0</span>] => <span class="hljs-keyword">Array</span> ( [id] => <span class="hljs-number">5</span> [name] => 智能机 [pid] => <span class="hljs-number">2</span> [children] => <span class="hljs-keyword">Array</span> ( ) ) [<span class="hljs-number">1</span>] => <span class="hljs-keyword">Array</span> ( [id] => <span class="hljs-number">6</span> [name] => 功能机 [pid] => <span class="hljs-number">2</span> [children] => <span class="hljs-keyword">Array</span> ( ) ) ) ) [<span class="hljs-number">3</span>] => <span class="hljs-keyword">Array</span> ( [id] => <span class="hljs-number">3</span> [name] => 笔记本 [pid] => <span class="hljs-number">1</span> [children] => <span class="hljs-keyword">Array</span> ( [<span class="hljs-number">0</span>] => <span class="hljs-keyword">Array</span> ( [id] => <span class="hljs-number">7</span> [name] => 超级本 [pid] => <span class="hljs-number">3</span> [children] => <span class="hljs-keyword">Array</span> ( ) ) [<span class="hljs-number">1</span>] => <span class="hljs-keyword">Array</span> ( [id] => <span class="hljs-number">8</span> [name] => 游戏本 [pid] => <span class="hljs-number">3</span> [children] => <span class="hljs-keyword">Array</span> ( ) ) ) ) [<span class="hljs-number">4</span>] => <span class="hljs-keyword">Array</span> ( [id] => <span class="hljs-number">4</span> [name] => 台式机 [pid] => <span class="hljs-number">1</span> [children] => <span class="hljs-keyword">Array</span> ( ) ) [<span class="hljs-number">5</span>] => <span class="hljs-keyword">Array</span> ( [id] => <span class="hljs-number">5</span> [name] => 智能机 [pid] => <span class="hljs-number">2</span> [children] => <span class="hljs-keyword">Array</span> ( ) ) [<span class="hljs-number">6</span>] => <span class="hljs-keyword">Array</span> ( [id] => <span class="hljs-number">6</span> [name] => 功能机 [pid] => <span class="hljs-number">2</span> [children] => <span class="hljs-keyword">Array</span> ( ) ) [<span class="hljs-number">7</span>] => <span class="hljs-keyword">Array</span> ( [id] => <span class="hljs-number">7</span> [name] => 超级本 [pid] => <span class="hljs-number">3</span> [children] => <span class="hljs-keyword">Array</span> ( ) ) [<span class="hljs-number">8</span>] => <span class="hljs-keyword">Array</span> ( [id] => <span class="hljs-number">8</span> [name] => 游戏本 [pid] => <span class="hljs-number">3</span> [children] => <span class="hljs-keyword">Array</span> ( ) ))</code>
优点:关系清楚,修改上下级关系简单。
缺点:使用PHP处理,如果分类数量庞大,效率也会降低。
原文 http://blog.csdn.net/u013927110/article/details/44086253

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

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

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

Many friends may be unfamiliar with the pid identifier, and you can check it in the task manager. However, some users cannot find the PID identifier when they open the Task Manager. In fact, if the user wants to view the process PID identifier, he or she needs to set the relevant settings of the "Task Manager" to see it. The following editor will take the win7 system as an example. How to view the process PID identifier. The PID identifier is a unique sequential number automatically assigned by the Windows operating system to running programs. After the process is terminated, the PID is recycled by the system and may continue to be assigned to newly running programs. When users need to view the process, they will use the task Manager to check, so how to check the process PID identifier? Let me share it with you below

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values of the same keys, making the program design more flexible. array_merge

BubblePal, a newly launched AI-based interactive toy, appears to be something that could have inspired the writers of the 2022 sci-fi/horror flick M3GAN, if it hadn’t just been launched last week. Based on large language model (LLM) technology, the ‘
