首页 > 代码库 > PHP递归重新排序无限级分类数组

PHP递归重新排序无限级分类数组

public static  function Menus($id,$spac=0){

/* $data = http://www.mamicode.com/array( 1 => array(‘id‘ => 1,‘name‘ => ‘name1‘,‘pid‘ => 0),
2 => array(‘id‘ => 2,‘name‘ => ‘name2‘,‘pid‘ => 0),
3 => array(‘id‘ => 3,‘name‘ => ‘name3‘,‘pid‘ => 1),
4 => array(‘id‘ => 4,‘name‘ => ‘name4‘,‘pid‘ => 0),
5 => array(‘id‘ => 5,‘name‘ => ‘name5‘,‘pid‘ => 3),
6 => array(‘id‘ => 6,‘name‘ => ‘name6‘,‘pid‘ => 3),
);*/
$menu = Menu::model()->findAll();

$arr = array();
// $arr = array_push($arr,$arr);
foreach ($menu as $k => $v) {
if ($v[‘parent_id‘] == $id) {
// $arr[] = $v;
//$v[‘id‘] =str_repeat(‘ ‘,$spac) .$v[‘name‘];
// $str .= "<option value=http://www.mamicode.com/‘" . $v[‘id‘] . "‘>" . str_repeat("&nbsp;",$spac) . "|--" . $v[‘name‘] . "</option>";
// self::Menus($v[‘id‘],$spac);
$arr[] = $v;
$arr = array_merge($arr,self::Menus($v[‘menu_id‘])); //递归合并数组

}

}
// print_r($arrs);
return $arr;
}

PHP递归重新排序无限级分类数组