Detailed explanation of PHP Infinitus classification
Infinitus classification simply means that a category can be divided into multiple subcategories, and then one subcategory can be divided into multiple other subcategories and so on indefinitely, just like Windows can create a new folder, and then in this folder You can also create some folders, and you can also create some folders under the folders.
So how does PHP achieve its infinite classification? How to list its various categories one by one?
First, let’s assume there is such an array
$arr = array( 0=>array( 'cid'=>1, 'pid'=>0, 'name'=>'亚洲', ), 1=>array( 'cid'=>2, 'pid'=>0, 'name'=>'北美洲', ), 2=>array( 'cid'=>3, 'pid'=>1, 'name'=>'中国', ), 3=>array( 'cid'=>4, 'pid'=>2, 'name'=>'美国', ), 4=>array( 'cid'=>5, 'pid'=>3, 'name'=>'北京', ), 5=>array( 'cid'=>6, 'pid'=>3, 'name'=>'河北', ), 6=>array( 'cid'=>7, 'pid'=>5, 'name'=>'东城区', ), 7=>array( 'cid'=>8, 'pid'=>5, 'name'=>'海淀区', ), );
We need a display similar to this as shown
If we want to If this array is displayed intuitively as shown in the picture above, it needs to be implemented using PHP recursion. So how to implement it? Look at the core code below
Copy code
private function GetTree($arr,$pid,$step){ global $tree; foreach($arr as $key=>$val) { if($val['pid'] == $pid) { $flg = str_repeat('└―',$step); $val['name'] = $flg.$val['name']; $tree[] = $val; $this->GetTree($arr , $val['cid'] ,$step+1); } } return $tree; }
Copy code
Then we only need to write a calling code
$newarr = $this->GetTree($arr, 0, 0);
Find out all the subclass ids Parent classification information (in yii2 framework)
Method 1:
public static function get_parent_list($arr,$id){ //$arr 所有分类列表 //$id 父级分类id static $list=array(); foreach($arr as $u){ if($u['id']== $id){//父级分类id等于所查找的id $list[]=$u; if($u['parent_id']>0){ self::get_parent_list($arr,$u['parent_id']); } } } return $list; }
This way, classification information can be stored in the cache without recursive query.
Method 2: Recursive query to obtain classification information
public static function get_parents($id){ static $list = []; $cat_data = Category::findOne($id)->toarray(); if($cat_data){ $list[] = $cat_data; $id = $cat_data['parent_id']; if($cat_data['parent_id'] > 0){ self::get_parents($id); } } return $list; }
Since we need to achieve infinite classification, we need to know what its sub-columns are?
The final array implemented is in the form:
[php] view plain copyArray ( [0] => Array ( [category_id] => 1 [category_name] => 关于我 [category_pid] => 0 [category_addtime] => 0 [category_order] => 0 ) [1] => Array ( [category_id] => 2 [category_name] => 生活随笔 [category_pid] => 0 [category_addtime] => 0 [category_order] => 0 ) [2] => Array ( [category_id] => 3 [category_name] => 文章类别 [category_pid] => 0 [category_addtime] => 0 [category_order] => 0 [category_child] => Array ( [0] => Array ( [category_id] => 4 [category_name] => Linux服务器 [category_pid] => 3 [category_addtime] => -28800 [category_order] => 1 [category_child] => Array ( [0] => Array ( [category_id] => 5 [category_name] => linux优化 [category_pid] => 4 [category_addtime] => 0 [category_order] => 0 ) [1] => Array ( [category_id] => 6 [category_name] => 虚拟化 [category_pid] => 4 [category_addtime] => 1478228220 [category_order] => 0 ) [2] => Array ( [category_id] => 7 [category_name] => Mysql优化与开发 [category_pid] => 4 [category_addtime] => 1478188800 [category_order] => 0 ) [3] => Array ( [category_id] => 8 [category_name] => 高可用与虚拟化 [category_pid] => 4 [category_addtime] => 1478394120 [category_order] => 0 ) ) ) [1] => Array ( [category_id] => 15 [category_name] => Mysql [category_pid] => 3 [category_addtime] => 1480555980 [category_order] => 0 [category_child] => Array ( [0] => Array ( [category_id] => 16 [category_name] => 优化 [category_pid] => 15 [category_addtime] => 1480555980 [category_order] => 0 ) [1] => Array ( [category_id] => 17 [category_name] => Sql编程 [category_pid] => 15 [category_addtime] => 1480556040 [category_order] => 8 ) ) ) ) ) )
So how can the two-dimensional array we take out from the database become an array like the above?
Example:
We want to implement the following two-dimensional array into the above form.
[php] view plain copy
$arr=array( array('id'=>'1','name'=>'北京','pid'=>'0'), array('id'=>'2','name'=>'上海','pid'=>'0'), array('id'=>'3','name'=>'浦东','pid'=>'2'), array('id'=>'4','name'=>'朝阳','pid'=>'1'), array('id'=>'5','name'=>'广州','pid'=>'0'), array('id'=>'6','name'=>'三里屯','pid'=>'4'), array('id'=>'7','name'=>'广东','pid'=>'5'), array('id'=>'8','name'=>'三里','pid'=>'4'), array('id'=>'10','name'=>'小胡同','pid'=>'8') );
The first method: Use the index of the array to be the same as the id number of the primary key to find the sub-column. First add the index to the array:
[php] view plain copy
$arr=array( 1=>array('id'=>'1','name'=>'北京','pid'=>'0'), 2=>array('id'=>'2','name'=>'上海','pid'=>'0'), 3=>array('id'=>'3','name'=>'浦东','pid'=>'2'), 4=>array('id'=>'4','name'=>'朝阳','pid'=>'1'), 5=>array('id'=>'5','name'=>'广州','pid'=>'0'), 6=>array('id'=>'6','name'=>'三里屯','pid'=>'4'), 7=>array('id'=>'7','name'=>'广东','pid'=>'5'), 8=>array('id'=>'8','name'=>'三里','pid'=>'4'), 10=>array('id'=>'10','name'=>'小胡同','pid'=>'8') );
Use the subscript of the index to determine whether the column has a parent column. If it exists, put the column into the son array of the parent column
[php] view plain copy
function generateTree($items){ $tree = array(); foreach($items as $item){ //判断是否有数组的索引== if(isset($items[$item['pid']])){ //查找数组里面是否有该分类 如 isset($items[0]) isset($items[1]) $items[$item['pid']]['son'][] = &$items[$item['id']]; //上面的内容变化,$tree里面的值就变化 }else{ $tree[] = &$items[$item['id']]; //把他的地址给了$tree } } return $tree; }
There is a major problem with the above program. Did we add the index of the array manually? So can we automatically add array indexes? Of course you can. The following program can be implemented. It automatically adds an index to the array and then puts the sub-column into the son array of the parent column. The idea is the same as the above program.
[php] view plain copy
function make_tree($list,$pk='id',$pid='pid',$child='_child',$root=0){ $tree=array(); $packData=array(); foreach ($list as $data) { <span style="white-space:pre"> </span>//转换为带有主键id的数组 $packData[$data[$pk]] = $data; //$packData[1]=$data; $packData[2]=$data } foreach ($packData as $key =>$val){ if($val[$pid]==$root){ //代表跟节点 $tree[]=& $packData[$key]; }else{ //找到其父类 $packData[$val[$pid]][$child][]=& $packData[$key]; } } return $tree; }
Second method: Use recursion to find sub-columns
Idea: Use recursion to find the sub-columns in each column, until the last sub-column is found, and then run the debugging layer by layer, You can print
tree. To facilitate understanding, you can print $tree to observe changes in the array.
[php] view plain copy
array('id'=>'6','name'=>'三里屯','pid'=>'4'), [php] view plain copyfunction make_tree1($list,$pk='id',$pid='pid',$child='_child',$root=0){ $tree=array(); foreach($list as $key=> $val){ if($val[$pid]==$root){ //获取当前$pid所有子类 unset($list[$key]); if(! empty($list)){ $child=make_tree1($list,$pk,$pid,$child,$val[$pk]); //来来来 找北京的子栏目 递归 空 if(!empty($child)){ $val['_child']=$child; } } $tree[]=$val; } } return $tree; }
In order to better understand recursion, you can find a small program to test, such as the following summation program
You can analyze the output of $n from 2, 3...10, instead of From 10, 9--2, I only understood how the recursion of the above program is implemented through this program. In short: recursion is a very interesting thing, I hope everyone can understand it well
[php] view plain copy
function sum($n){ $a=0; if($n>1){ $a=sum($n-1)+$n; /*if($n>5){ echo "$n <br />" ; }*/ echo "$n<br />"; }else{ $a=1; } return $a; } echo sum(10);
Get the data of all subordinate subclass ids based on the parent id
#$id = 父级id, $array = 所有分类public function getSon($id,$array){ static $list; foreach ($array as $k => $v) { if($v['parent_id'] == $id){ $list[] = $array[$k]; self::getSon($v['id'],$array); } } return $list; }
php Infinitus Classification
Here is the first introduction , what is the infinite classification?
Infinitus Classification To put it simply, a category can be divided into multiple subcategories, and then one subcategory can be divided into multiple other subcategories and so on indefinitely, just like Windows can create a new folder, and then in this You can create some folders in the folder, and you can also create some folders under the folder.
So how does PHP achieve its infinite classification? How to list its various categories one by one?
First, let’s assume there is such an array
$arr = array( 0=>array( 'cid'=>1, 'pid'=>0, 'name'=>'亚洲', ), 1=>array( 'cid'=>2, 'pid'=>0, 'name'=>'北美洲', ), 2=>array( 'cid'=>3, 'pid'=>1, 'name'=>'中国', ), 3=>array( 'cid'=>4, 'pid'=>2, 'name'=>'美国', ), 4=>array( 'cid'=>5, 'pid'=>3, 'name'=>'北京', ), 5=>array( 'cid'=>6, 'pid'=>3, 'name'=>'河北', ), 6=>array( 'cid'=>7, 'pid'=>5, 'name'=>'东城区', ), 7=>array( 'cid'=>8, 'pid'=>5, 'name'=>'海淀区', ), );
We need a display similar to this as shown
If we want to If this array is displayed intuitively as shown in the picture above, it needs to be implemented using PHP recursion. So how to implement it? Look at the core code below
Copy code
private function GetTree($arr,$pid,$step){ global $tree; foreach($arr as $key=>$val) { if($val['pid'] == $pid) { $flg = str_repeat('└―',$step); $val['name'] = $flg.$val['name']; $tree[] = $val; $this->GetTree($arr , $val['cid'] ,$step+1); } } return $tree; }
Copy code
Then we only need to write a calling code
$newarr = $this->GetTree($arr, 0, 0);
Find out all the subclass ids Parent classification information (in yii2 framework)
Method 1:
public static function get_parent_list($arr,$id){ //$arr 所有分类列表 //$id 父级分类id static $list=array(); foreach($arr as $u){ if($u['id']== $id){//父级分类id等于所查找的id $list[]=$u; if($u['parent_id']>0){ self::get_parent_list($arr,$u['parent_id']); } } } return $list; }
This way, classification information can be stored in the cache without recursive query.
Method 2: Recursive query to obtain classification information
public static function get_parents($id){ static $list = []; $cat_data = Category::findOne($id)->toarray(); if($cat_data){ $list[] = $cat_data; $id = $cat_data['parent_id']; if($cat_data['parent_id'] > 0){ self::get_parents($id); } } return $list; }
Since we need to achieve infinite classification, we need to know what its sub-columns are?
The final array implemented is in the form:
[php] view plain copyArray ( [0] => Array ( [category_id] => 1 [category_name] => 关于我 [category_pid] => 0 [category_addtime] => 0 [category_order] => 0 ) [1] => Array ( [category_id] => 2 [category_name] => 生活随笔 [category_pid] => 0 [category_addtime] => 0 [category_order] => 0 ) [2] => Array ( [category_id] => 3 [category_name] => 文章类别 [category_pid] => 0 [category_addtime] => 0 [category_order] => 0 [category_child] => Array ( [0] => Array ( [category_id] => 4 [category_name] => Linux服务器 [category_pid] => 3 [category_addtime] => -28800 [category_order] => 1 [category_child] => Array ( [0] => Array ( [category_id] => 5 [category_name] => linux优化 [category_pid] => 4 [category_addtime] => 0 [category_order] => 0 ) [1] => Array ( [category_id] => 6 [category_name] => 虚拟化 [category_pid] => 4 [category_addtime] => 1478228220 [category_order] => 0 ) [2] => Array ( [category_id] => 7 [category_name] => Mysql优化与开发 [category_pid] => 4 [category_addtime] => 1478188800 [category_order] => 0 ) [3] => Array ( [category_id] => 8 [category_name] => 高可用与虚拟化 [category_pid] => 4 [category_addtime] => 1478394120 [category_order] => 0 ) ) ) [1] => Array ( [category_id] => 15 [category_name] => Mysql [category_pid] => 3 [category_addtime] => 1480555980 [category_order] => 0 [category_child] => Array ( [0] => Array ( [category_id] => 16 [category_name] => 优化 [category_pid] => 15 [category_addtime] => 1480555980 [category_order] => 0 ) [1] => Array ( [category_id] => 17 [category_name] => Sql编程 [category_pid] => 15 [category_addtime] => 1480556040 [category_order] => 8 ) ) ) ) ) )
So how can the two-dimensional array we take out from the database become an array like the above?
Example:
We want to implement the following two-dimensional array into the above form.
[php] view plain copy
$arr=array( array('id'=>'1','name'=>'北京','pid'=>'0'), array('id'=>'2','name'=>'上海','pid'=>'0'), array('id'=>'3','name'=>'浦东','pid'=>'2'), array('id'=>'4','name'=>'朝阳','pid'=>'1'), array('id'=>'5','name'=>'广州','pid'=>'0'), array('id'=>'6','name'=>'三里屯','pid'=>'4'), array('id'=>'7','name'=>'广东','pid'=>'5'), array('id'=>'8','name'=>'三里','pid'=>'4'), array('id'=>'10','name'=>'小胡同','pid'=>'8') );
The first method: use the index of the array to be the same as the id number of the primary key to find the sub-column, first add the index to the array:
[php] view plain copy
$arr=array( 1=>array('id'=>'1','name'=>'北京','pid'=>'0'), 2=>array('id'=>'2','name'=>'上海','pid'=>'0'), 3=>array('id'=>'3','name'=>'浦东','pid'=>'2'), 4=>array('id'=>'4','name'=>'朝阳','pid'=>'1'), 5=>array('id'=>'5','name'=>'广州','pid'=>'0'), 6=>array('id'=>'6','name'=>'三里屯','pid'=>'4'), 7=>array('id'=>'7','name'=>'广东','pid'=>'5'), 8=>array('id'=>'8','name'=>'三里','pid'=>'4'), 10=>array('id'=>'10','name'=>'小胡同','pid'=>'8') );
利用索引的下标,判断该栏目是否存在父栏目,如果存在就把该栏目放到父栏目的son子数组里面
[php] view plain copy
function generateTree($items){ $tree = array(); foreach($items as $item){ //判断是否有数组的索引== if(isset($items[$item['pid']])){ //查找数组里面是否有该分类 如 isset($items[0]) isset($items[1]) $items[$item['pid']]['son'][] = &$items[$item['id']]; //上面的内容变化,$tree里面的值就变化 }else{ $tree[] = &$items[$item['id']]; //把他的地址给了$tree } } return $tree; }
上面这个程序有一个主要问题,数组的索引是我们手动添加的?那我们可不可以自动添加数组索引呢?当然可以,下面这个程序就实现了,自动为数组添加索引然后再把子栏目放到父栏目的son数组中,思想和上面的程序是一样的
[php] view plain copy
function make_tree($list,$pk='id',$pid='pid',$child='_child',$root=0){ $tree=array(); $packData=array(); foreach ($list as $data) { <span style="white-space:pre"> </span>//转换为带有主键id的数组 $packData[$data[$pk]] = $data; //$packData[1]=$data; $packData[2]=$data } foreach ($packData as $key =>$val){ if($val[$pid]==$root){ //代表跟节点 $tree[]=& $packData[$key]; }else{ //找到其父类 $packData[$val[$pid]][$child][]=& $packData[$key]; } } return $tree; }
第二种方法:使用递归来查找子栏目
思想:使用递归来查找每个栏目中的子栏目,直到找到最后一个子栏目以后再一层层往外运行调试的话,可以打印
tree中,,为了方便理解,可以打印$tree去观察数组的变化
[php] view plain copy
array('id'=>'6','name'=>'三里屯','pid'=>'4'), [php] view plain copyfunction make_tree1($list,$pk='id',$pid='pid',$child='_child',$root=0){ $tree=array(); foreach($list as $key=> $val){ if($val[$pid]==$root){ //获取当前$pid所有子类 unset($list[$key]); if(! empty($list)){ $child=make_tree1($list,$pk,$pid,$child,$val[$pk]); //来来来 找北京的子栏目 递归 空 if(!empty($child)){ $val['_child']=$child; } } $tree[]=$val; } } return $tree; }
为了更好的理解递归,大家可以找一个小程序测试一下例如下面这个求和的程序
大家可以分析一下$n的输出从2,3 …..10,而不是从10,9—-2,我从这个程序才理解了一下上面那个程序的递归是如何实现的,总之:递归是很有意思的一个东西,希望大家好好理解
[php] view plain copy
function sum($n){ $a=0; if($n>1){ $a=sum($n-1)+$n; /*if($n>5){ echo "$n <br />" ; }*/ echo "$n<br />"; }else{ $a=1; } return $a; } echo sum(10);
根据父id获得所有下级子类id的数据
#$id = 父级id, $array = 所有分类public function getSon($id,$array){ static $list; foreach ($array as $k => $v) { if($v['parent_id'] == $id){ $list[] = $array[$k]; self::getSon($v['id'],$array); } } return $list; }
相关推荐:
The above is the detailed content of Detailed explanation of PHP Infinitus classification. For more information, please follow other related articles on the PHP Chinese website!

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

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
