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

PHP infinite classification (recursive function) code

WBOY
WBOYOriginal
2016-07-25 08:56:261212browse
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;
}


Statement:
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