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 |