首页  >  文章  >  web前端  >  在循环里如何使用<optgroup>标签

在循环里如何使用<optgroup>标签

黄舟
黄舟原创
2017-07-03 09:43:442037浏览

如何在循环里插入5b7a15bed8615d1b843806256bebea72?

$data = array (
  0 => 
  array (
    &#39;id&#39; => &#39;1&#39;,
    &#39;name&#39; => &#39;WEB编程&#39;,
    &#39;parentid&#39; => &#39;0&#39;,
  ),
  1 => 
  array (
    &#39;id&#39; => &#39;2&#39;,
    &#39;name&#39; => &#39;PHP&#39;,
    &#39;parentid&#39; => &#39;1&#39;,
  ),
  2 => 
  array (
    &#39;id&#39; => &#39;3&#39;,
    &#39;name&#39; => &#39;Ajax&#39;,
    &#39;parentid&#39; => &#39;1&#39;,
  ),
  3 => 
  array (
    &#39;id&#39; => &#39;4&#39;,
    &#39;name&#39; => &#39;java&#39;,
    &#39;parentid&#39; => &#39;1&#39;,
  ),
  4 => 
  array (
    &#39;id&#39; => &#39;5&#39;,
    &#39;name&#39; => &#39;WinForm编程&#39;,
    &#39;parentid&#39; => &#39;0&#39;,
  ),
  5 => 
  array (
    &#39;id&#39; => &#39;6&#39;,
    &#39;name&#39; => &#39;VB&#39;,
    &#39;parentid&#39; => &#39;5&#39;,
  ),
  6 => 
  array (
    &#39;id&#39; => &#39;7&#39;,
    &#39;name&#39; => &#39;VC&#39;,
    &#39;parentid&#39; => &#39;5&#39;,
  ),
);

这么一个数组,想得到下面的效果。

<select name="categorys">
    <optgroup label="WEB编程">
        <option value="2" >PHP</option>
        <option value="3" >Ajax</option>
        <option value="4" >java</option>
    </optgroup>
 
    <optgroup label="WinForm编程">
        <option value="6" >VB</option>
        <option value="7" >VC</option>
    </optgroup>
</select>

难点在于如何在foreach里判断把5a07473c87748fb1bf73f23d45547ab8包含在5b7a15bed8615d1b843806256bebea72里,折腾了半天也没出办法。请高手指点如何完成。

$data = array (
  0 =>  array (
    &#39;id&#39; => &#39;1&#39;,
    &#39;name&#39; => &#39;WEB编程&#39;,
    &#39;parentid&#39; => &#39;0&#39;,
  ),
  1 => array (
    &#39;id&#39; => &#39;2&#39;,
    &#39;name&#39; => &#39;PHP&#39;,
    &#39;parentid&#39; => &#39;1&#39;,
  ),
  2 => array (
    &#39;id&#39; => &#39;3&#39;,
    &#39;name&#39; => &#39;Ajax&#39;,
    &#39;parentid&#39; => &#39;1&#39;,
  ),
  3 => array (
    &#39;id&#39; => &#39;4&#39;,
    &#39;name&#39; => &#39;java&#39;,
    &#39;parentid&#39; => &#39;1&#39;,
  ),
  4 => array (
    &#39;id&#39; => &#39;5&#39;,
    &#39;name&#39; => &#39;WinForm编程&#39;,
    &#39;parentid&#39; => &#39;0&#39;,
  ),
  5 => array (
    &#39;id&#39; => &#39;6&#39;,
    &#39;name&#39; => &#39;VB&#39;,
    &#39;parentid&#39; => &#39;5&#39;,
  ),
  6 => array (
    &#39;id&#39; => &#39;7&#39;,
    &#39;name&#39; => &#39;VC&#39;,
    &#39;parentid&#39; => &#39;5&#39;,
  ),
);
 
echo &#39;<select name="categorys">&#39;;
foreach($data as $v) {
  if($v[&#39;parentid&#39;] == 0)
    echo "<optgroup label=&#39;$v[name]&#39;>";
  else
    echo "<option value=&#39;$v[id]&#39; >$v[name]</option>";
}
echo &#39;</select>&#39;;

唠叨大哥,你上面的操作还是缺了结束符啊bc2752b74169f27a1fb12a4976198c73

我就是在苦闷如何把这个bc2752b74169f27a1fb12a4976198c73加进去。

echo "<optgroup label=&#39;$v[name]&#39;>$v[name]</optgroup>";

稍微改下就行了。

$data = array (
  0 => 
  array (
    &#39;id&#39; => &#39;1&#39;,
    &#39;name&#39; => &#39;WEB编程&#39;,
    &#39;parentid&#39; => &#39;0&#39;,
  ),
  1 => 
  array (
    &#39;id&#39; => &#39;2&#39;,
    &#39;name&#39; => &#39;PHP&#39;,
    &#39;parentid&#39; => &#39;1&#39;,
  ),
  2 => 
  array (
    &#39;id&#39; => &#39;3&#39;,
    &#39;name&#39; => &#39;Ajax&#39;,
    &#39;parentid&#39; => &#39;1&#39;,
  ),
  3 => 
  array (
    &#39;id&#39; => &#39;4&#39;,
    &#39;name&#39; => &#39;java&#39;,
    &#39;parentid&#39; => &#39;1&#39;,
  ),
  4 => 
  array (
    &#39;id&#39; => &#39;5&#39;,
    &#39;name&#39; => &#39;WinForm编程&#39;,
    &#39;parentid&#39; => &#39;0&#39;,
  ),
  5 => 
  array (
    &#39;id&#39; => &#39;6&#39;,
    &#39;name&#39; => &#39;VB&#39;,
    &#39;parentid&#39; => &#39;5&#39;,
  ),
  6 => 
  array (
    &#39;id&#39; => &#39;7&#39;,
    &#39;name&#39; => &#39;VC&#39;,
    &#39;parentid&#39; => &#39;5&#39;,
  ),
);
$tempArray = array();
foreach($data as $item){
    $tempArray[$item['parentid']][$item['id']] = $item['name'];
}
echo '';

只是foreach了三次怕效率较差,不知还没有有别的方法。

重做数组的话……

$data = array (
  array (
    &#39;id&#39; => &#39;1&#39;,
    &#39;name&#39; => &#39;WEB编程&#39;,
    &#39;parentid&#39; => &#39;0&#39;,
    &#39;sub&#39;=>
     array(
          array(
            &#39;id&#39; => &#39;2&#39;,
            &#39;name&#39; => &#39;PHP&#39;,
            &#39;parentid&#39; => &#39;1&#39;,
          ),
          array (
            &#39;id&#39; => &#39;3&#39;,
            &#39;name&#39; => &#39;Ajax&#39;,
            &#39;parentid&#39; => &#39;1&#39;,
          ),
          array (
            &#39;id&#39; => &#39;4&#39;,
            &#39;name&#39; => &#39;java&#39;,
            &#39;parentid&#39; => &#39;1&#39;,
          )
    )
  ),
  array (
    &#39;id&#39; => &#39;5&#39;,
    &#39;name&#39; => &#39;WinForm编程&#39;,
    &#39;parentid&#39; => &#39;0&#39;,
    &#39;sub&#39;=>
    array(
          array (
            &#39;id&#39; => &#39;6&#39;,
            &#39;name&#39; => &#39;VB&#39;,
            &#39;parentid&#39; => &#39;5&#39;,
          ),
          array (
            &#39;id&#39; => &#39;7&#39;,
            &#39;name&#39; => &#39;VC&#39;,
            &#39;parentid&#39; => &#39;5&#39;,
          )
    )
  )
);
echo &#39;<select style="width:200px;">&#39;;
for($i=0,$il=count($data);$i<$il;$i++){
    echo &#39;<optgroup label="&#39;.$data[$i][&#39;name&#39;].&#39;">&#39;;
    for($j=0,$jl=count($data[$i][&#39;sub&#39;]);$j<$jl;$j++){
        echo &#39;<option>&#39;.$data[$i][&#39;sub&#39;][$j][&#39;name&#39;].&#39;</option>&#39;;
    }
    echo &#39;<optgroup>&#39;;
}
echo &#39;</select>&#39;;

   是吗?
哦,是少了个 bc2752b74169f27a1fb12a4976198c73
不过我测试代码时并没有感觉出有与没有的差异

一定要有的话,就加个开关变量。要多次循环干什么?

echo &#39;<select name="categorys">&#39;;
$k = false;
foreach($data as $v) {
  if($v[&#39;parentid&#39;] == 0) {
    echo "<optgroup label=&#39;$v[name]&#39;>";
    $k = true;
  }else {
    if($k) echo &#39;</optgroup>&#39;;
    echo "<option value=&#39;$v[id]&#39; >$v[name]</option>";
    $k = false;
  }
}
echo &#39;</select>&#39;;


以上是在循环里如何使用<optgroup>标签的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn