How to use tag in loop

黄舟
Release: 2017-07-03 09:43:44
Original
2019 people have browsed it

How to insert 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',
  ),
);
Copy after login

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>
Copy after login

The difficulty lies in how to judge whether 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 (
    &#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;;
Copy after login

Nagging brother, the above operation still lacks the end character

I am just wondering how to add this.

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

Just change it a little bit.

$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 '';
Copy after login

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 (
    &#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;;
Copy after login

Really?
Oh, it is missing
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 &#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;;
Copy after login


The above is the detailed content of How to use tag in loop. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!