Home > Article > Web Front-end > How to use
How to insert 5b7a15bed8615d1b843806256bebea72 in loop?
$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', ), );
With such an array, I want to get the following effect.
<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>
The difficulty lies in how to judge whether 5a07473c87748fb1bf73f23d45547ab8 is included in 5b7a15bed8615d1b843806256bebea72 in foreach. After struggling for a long time, I still can’t find a solution. Please give some guidance on how to complete it.
$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>';
Nagging brother, the above operation still lacks the end characterbc2752b74169f27a1fb12a4976198c73
I am just wondering how to add thisbc2752b74169f27a1fb12a4976198c73.
echo "<optgroup label='$v[name]'>$v[name]</optgroup>";
Just change it a little bit.
$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 '';
I just foreach three times for fear of poor efficiency. I don’t know if there is any other method.
If you redo the array...
$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>';
Really?
Oh, it is missing bc2752b74169f27a1fb12a4976198c73
But when I tested the code, I didn’t feel the difference between yes and no
If it must be there, just add a switch variable. Why do we need to cycle multiple times?
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>';
The above is the detailed content of How to use