PHP递归算法
/**
* 获取菜单
* @param number $id
* @return multitype:
*/
public function menu($id = 0) {
$menu = M ( 'menu' );
$arrlist = array (); // 数组必须初始化,否push不进去
$where ['app'] = 'Portal';
$where ['parentid'] = $id;
$where ['status'] = 1;
$parent = $menu->order ( 'listorder asc' )->where ( $where )->select ();//判断是否包含children元素
for($i = 0; $i < count ( $parent ); $i ++) {
$arr ['name'] = $parent [$i] ['name'];
$arr ['id'] = $parent [$i] ['id'];
if($id!=0)
{
$arr ['link'] = U ( $parent [$i] ['app'] . '/' . $parent [$i] ['model'] . '/' . $parent [$i] ['action'] . '/' . $parent [$i] ['data'] . '/tid/' . $parent [$i] ['parentid'] . '/id/' . $parent [$i] ['id'] );
}
else
{
$arr ['link'] = U ( $parent [$i] ['app'] . '/' . $parent [$i] ['model'] . '/' . $parent [$i] ['action'] . '/' . $parent [$i] ['data'] . '/tid/' . $parent [$i] ['id'] );
}
$arr ['child'] = $this->menu ( $parent [$i] ['id'] );
array_push ( $arrlist, $arr ); //执行完成这个方法后继续进行下一次递归
}
return $arrlist; // 一次递归完成 然后执行 array_push方法
}