Home >Backend Development >PHP Tutorial >PHP recursive traversal to achieve infinite classification
This article mainly introduces PHP recursive traversal to achieve infinite classification. Interested friends can refer to it. I hope it will be helpful to everyone.
Infinite-level classification is a classification technique. For example, department organization, article classification, subject classification, etc. often use infinite-level classification. It can be simply understood as classification. In fact, if we think about it carefully, there are simply too many classifications in life. Clothes can be divided into men's clothing and women's clothing, tops and pants, and they can also be classified according to age groups. Classification is everywhere, and classification appears "infinite". I won’t talk about the necessity of infinite classification here.
The example of this article describes the method of php recursion to achieve infinite classification, the details are as follows:
<?php $rows = array( array( 'id' => 1, 'name' => 'dev', 'parentid' => 0 ), array( 'id' => 2, 'name' => 'php', 'parentid' => 1 ), array( 'id' => 3, 'name' => 'smarty', 'parentid' => 2 ), array( 'id' => 4, 'name' => 'life', 'parentid' => 0 ), array( 'id' => 5, 'name' => 'pdo', 'parentid' => 2 ), array( 'id' => 6, 'name' => 'pdo-mysql', 'parentid' => 5 ), array( 'id' => 7, 'name' => 'java', 'parentid' => 1 ) ); // 72648 // 84072 function findChild(&$arr,$id){ $childs=array(); foreach ($arr as $k => $v){ if($v['parentid']== $id){ $childs[]=$v; } } return $childs; } function build_tree($root_id){ global $rows; $childs=findChild($rows,$root_id); if(empty($childs)){ return null; } foreach ($childs as $k => $v){ $rescurTree=build_tree($v[id]); if( null != $rescurTree){ $childs[$k]['childs']=$rescurTree; } } return $childs; } $tree=build_tree(0); echo memory_get_usage(); print_r($tree); ?>
Summary: The above is the entire content of this article, I hope it can be helpful to everyone Learning helps.
Related recommendations:
Definition and generation method of PHP barcode
php determines and obtains the file extension Several methods
php image file operation encapsulation class
The above is the detailed content of PHP recursive traversal to achieve infinite classification. For more information, please follow other related articles on the PHP Chinese website!