首頁 > web前端 > html教學 > 在循環裡如何使用標籤

在循環裡如何使用標籤

黄舟
發布: 2017-07-03 09:43:44
原創
2056 人瀏覽過

如何在循環裡插入

$data = array (
  0 => 
  array (
    'id' => '1',
    'name' => 'WEB编程',
    'parentid' => '0',
  ),
  1 => 
  array (
    'id' => '2',
    'name' => 'PHP',
    'parentid' => '1',
  ),
  2 => 
  array (
    'id' => '3',
    'name' => 'Ajax',
    'parentid' => '1',
  ),
  3 => 
  array (
    'id' => '4',
    'name' => 'java',
    'parentid' => '1',
  ),
  4 => 
  array (
    'id' => '5',
    'name' => 'WinForm编程',
    'parentid' => '0',
  ),
  5 => 
  array (
    'id' => '6',
    'name' => 'VB',
    'parentid' => '5',
  ),
  6 => 
  array (
    'id' => '7',
    'name' => 'VC',
    'parentid' => '5',
  ),
);
登入後複製

這麼一個陣列,想得到下面的效果。

<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裡判斷把裡,折騰了半天也沒出辦法。請高手指點如何完成。

$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;;
登入後複製

嘮叨大哥,你上面的操作還是缺了結束符啊

我就是在苦悶如何把這個加進去。

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;;
登入後複製

   是嗎?
哦,是少了個 
不過我測試程式碼時並沒有感覺出有與沒有的差異

一定要有的話,就加個開關變數。要多次循環幹什麼?

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;;
登入後複製


以上是在循環裡如何使用標籤的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板