如何在循環裡插入
$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 ( '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', ), ); echo '<select name="categorys">'; foreach($data as $v) { if($v['parentid'] == 0) echo "<optgroup label='$v[name]'>"; else echo "<option value='$v[id]' >$v[name]</option>"; } echo '</select>';
嘮叨大哥,你上面的操作還是缺了結束符啊
我就是在苦悶如何把這個加進去。
echo "<optgroup label='$v[name]'>$v[name]</optgroup>";
稍微改下來就行了。
$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', ), ); $tempArray = array(); foreach($data as $item){ $tempArray[$item['parentid']][$item['id']] = $item['name']; } echo '';
只是foreach了三次怕效率較差,不知還沒有有別的方法。
重做陣列的話…
$data = array ( array ( 'id' => '1', 'name' => 'WEB编程', 'parentid' => '0', 'sub'=> array( array( 'id' => '2', 'name' => 'PHP', 'parentid' => '1', ), array ( 'id' => '3', 'name' => 'Ajax', 'parentid' => '1', ), array ( 'id' => '4', 'name' => 'java', 'parentid' => '1', ) ) ), array ( 'id' => '5', 'name' => 'WinForm编程', 'parentid' => '0', 'sub'=> array( array ( 'id' => '6', 'name' => 'VB', 'parentid' => '5', ), array ( 'id' => '7', 'name' => 'VC', 'parentid' => '5', ) ) ) ); echo '<select style="width:200px;">'; for($i=0,$il=count($data);$i<$il;$i++){ echo '<optgroup label="'.$data[$i]['name'].'">'; for($j=0,$jl=count($data[$i]['sub']);$j<$jl;$j++){ echo '<option>'.$data[$i]['sub'][$j]['name'].'</option>'; } echo '<optgroup>'; } echo '</select>';
是嗎?
哦,是少了個
不過我測試程式碼時並沒有感覺出有與沒有的差異
一定要有的話,就加個開關變數。要多次循環幹什麼?
echo '<select name="categorys">'; $k = false; foreach($data as $v) { if($v['parentid'] == 0) { echo "<optgroup label='$v[name]'>"; $k = true; }else { if($k) echo '</optgroup>'; echo "<option value='$v[id]' >$v[name]</option>"; $k = false; } } echo '</select>';
以上是在循環裡如何使用