Home > Article > Backend Development > PHP infinite classification recursive function implementation
/**
*
* @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;
}