'1', "caption" => "一级菜单1"),   array("level" => '1',&"/>  '1', "caption" => "一级菜单1"),   array("level" => '1',&">

Heim  >  Artikel  >  Backend-Entwicklung  >  请问版主xuzuning

请问版主xuzuning

WBOY
WBOYOriginal
2016-06-13 12:56:15773Durchsuche

请教版主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 />


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


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

只要不为空,就表示取到了。取到了当然就是 true 了
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn