Detailed explanation of PHP recursion and infinite classification examples

墨辰丷
Release: 2023-03-31 22:02:02
Original
2264 people have browsed it

This article mainly introduces the method of realizing recursion and infinite classification in PHP, involving the recursive operation skills of PHP. Friends who need it can refer to it

The example of this article describes the method of realizing recursion and infinite classification in PHP.

The specific implementation method is as follows:

<?php
echo "<pre class="brush:php;toolbar:false">";
$area = array(
array(&#39;id&#39;=>1,&#39;area&#39;=>&#39;北京&#39;,&#39;pid&#39;=>0),
array(&#39;id&#39;=>2,&#39;area&#39;=>&#39;广西&#39;,&#39;pid&#39;=>0),
array(&#39;id&#39;=>3,&#39;area&#39;=>&#39;广东&#39;,&#39;pid&#39;=>0),
array(&#39;id&#39;=>4,&#39;area&#39;=>&#39;福建&#39;,&#39;pid&#39;=>0),
array(&#39;id&#39;=>11,&#39;area&#39;=>&#39;朝阳区&#39;,&#39;pid&#39;=>1),
array(&#39;id&#39;=>12,&#39;area&#39;=>&#39;海淀区&#39;,&#39;pid&#39;=>1),
array(&#39;id&#39;=>21,&#39;area&#39;=>&#39;南宁市&#39;,&#39;pid&#39;=>2),
array(&#39;id&#39;=>45,&#39;area&#39;=>&#39;福州市&#39;,&#39;pid&#39;=>4),
array(&#39;id&#39;=>113,&#39;area&#39;=>&#39;亚运村&#39;,&#39;pid&#39;=>11),
array(&#39;id&#39;=>115,&#39;area&#39;=>&#39;奥运村&#39;,&#39;pid&#39;=>11),
array(&#39;id&#39;=>234,&#39;area&#39;=>&#39;武鸣县&#39;,&#39;pid&#39;=>21)
); 

function t($arr,$pid=0,$lev=0){
 static $list = array();
 foreach($arr as $v){
 if($v[&#39;pid&#39;]==$pid){
  echo str_repeat("  ",$lev).$v[&#39;area&#39;]."<br />";
  //这里输出,是为了看效果
  $list[] = $v;
  t($arr,$v[&#39;id&#39;],$lev+1);
 } 
 }
 return $list;
}
$list = t($area);
echo "<hr >";
print_r($list);
?>
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.

Related recommendations:

Mysql operation buffer method in php

Definition and usage of mail function in php

phpHow to get the array passed from the html form

The above is the detailed content of Detailed explanation of PHP recursion and infinite classification examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template