日期:2014-05-17 浏览次数:20575 次
$ar = array (
array("level" => '1', "caption" => "一级菜单1"),
array("level" => '1', "caption" => "一级菜单2"),
array("level" => '2', "caption" => "二级菜单1"),
array("level" => '2', "caption" => "二级菜单2"),
array("level" => '3', "caption" => "三级菜单1"),
array("level" => '1', "caption" => "一级菜单3"),
);
print_r( foo($ar));
function foo(&$ar, $level='1') {
$res = array();
while($r = current($ar)) {
if($r['level'] > $level) $res[count($res)-1]['sub'] = foo($ar, $r['level']);
elseif($r['level'] == $level) $res[] = $r;
else {
array_unshift($ar, $r);
break;
}
array_shift($ar);
}
return $res;
}