Home > Backend Development > PHP Tutorial > PHP infinite classification (recursive function) code

PHP infinite classification (recursive function) code

WBOY
Release: 2016-07-25 08:56:26
Original
1199 people have browsed it
This article introduces an infinite classification code implemented by PHP with the help of recursive functions. Friends in need can refer to it.

php Infinitus classification, recursive function implementation, the code is as follows:

<?php
php 
/**
 * 
 * @param 所有数组 $array
 * @param 当前用户ID $id
 * @param 储存变量 $str
 * @return string
 */
function findIds($array,$id,$str='') {
$result = findChild($array,$id);//取得当前节点下的所有同级子节点
foreach ($result as $k => $v){
// 赋值给变量
$str.=$v['id'].',';
//再次调用这个函数显示子节点下的同级子节点
findIds($array,$v['id'],&$str);
 
}
return $str;//返回变量
}
//取得当前节点下的所有同级子节点
function findChild(&$arr,$id){
$childs=array();
foreach ($arr as $k => $v){
if($v['pid']== $id){
$childs[]=$v;
}
}
return $childs;
}
Copy after login


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