'1', "caption" => "一级菜单1"),   array("level" => '1',&"/>  '1', "caption" => "一级菜单1"),   array("level" => '1',&">
Home > Backend Development > PHP Tutorial > 请问版主xuzuning

请问版主xuzuning

WBOY
Release: 2016-06-13 12:56:15
Original
809 people have browsed it

请教版主xuzuning

<br />
$ar = array (<br />
  array("level" => '1', "caption" => "一级菜单1"),<br />
  array("level" => '1', "caption" => "一级菜单2"),<br />
  array("level" => '2', "caption" => "二级菜单1"),<br />
  array("level" => '2', "caption" => "二级菜单2"),<br />
  array("level" => '3', "caption" => "三级菜单1"),<br />
  array("level" => '1', "caption" => "一级菜单3"),<br />
);<br />
print_r( foo($ar));<br />
<br />
function foo(&$ar, $level='1') {<br />
  $res = array();<br />
  while($r = current($ar)) {<br />
    if($r['level'] > $level) $res[count($res)-1]['sub'] = foo($ar, $r['level']);<br />
    elseif($r['level'] == $level) $res[] = $r;<br />
    else {<br />
      array_unshift($ar, $r);<br />
      break;<br />
    }<br />
    array_shift($ar);<br />
  }<br />
  return $res;<br />
}<br />
Copy after login


在上面的代码中while($r = current($ar)) 是如何为true的,有点搞不明白


------解决方案--------------------
current($ar) 是取数组 $ar 的当前元素

只要不为空,就表示取到了。取到了当然就是 true 了
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template