Home >Backend Development >PHP Tutorial >PHP infinite classification recursive function implementation

PHP infinite classification recursive function implementation

巴扎黑
巴扎黑Original
2016-11-22 09:29:241267browse

/**

*

* @param All arrays $array

* @param Current user ID $id

* @param Storage variables $str

* @return string

*/

function findIds($array,$id,$str='') {

$result = findChild($array,$id);//Get all the same items under the current node Level child node

foreach ($result as $k => $v){

//Assign value to variable

$str.=$v['id'].',';

//Call again This function displays the sibling child nodes under the child node

findIds($array,$v['id'],&$str);

}

return $str; //Return variable

}

//Get all sibling child nodes under the current node

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