search
HomeWeb Front-endHTML TutorialHow to use tag in loop
How to use tag in loopJul 03, 2017 am 09:43 AM
useLabel

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',
  ),
);

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 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;;

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>";

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 '';

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;;

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;;


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!

Statement
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
如何在Chrome和Edge的所有选项卡中搜索文本如何在Chrome和Edge的所有选项卡中搜索文本Feb 19, 2024 am 11:30 AM

本教程向您展示了如何在Windows的Chrome或Edge中找到所有打开的标签页上的特定文本或短语。有没有办法在Chrome中所有打开的标签页上进行文本搜索?是的,您可以使用Chrome中的免费外部Web扩展在所有打开的标签上执行文本搜索,无需手动切换标签。一些扩展如TabSearch和Ctrl-FPlus可以帮助您轻松实现这一功能。如何在GoogleChrome的所有选项卡中搜索文本?Ctrl-FPlus是一个免费的扩展,它方便用户在浏览器窗口的所有选项卡中搜索特定的单词、短语或文本。这个扩

如何在Go中使用第三方库?如何在Go中使用第三方库?May 11, 2023 pm 03:30 PM

在Go语言中,使用第三方库是非常方便的。许多优秀的第三方库和框架可以帮助我们快速地开发应用程序,同时也减少了我们自己编写代码的工作量。但是如何正确地使用第三方库,确保其稳定性和可靠性,是我们必须了解的一个问题。本文将从以下几个方面介绍如何使用第三方库,并结合具体例子进行讲解。一、第三方库的获取Go语言中获取第三方库有以下两种方式:1.使用goget命令首先

抖音怎么带标签引流?平台什么标签最容易引流?抖音怎么带标签引流?平台什么标签最容易引流?Mar 22, 2024 am 10:28 AM

抖音作为一款备受欢迎的短视频社交平台,拥有着庞大的用户群体。对于抖音创作者来说,带标签引流是一种有效提升内容曝光度和吸引关注的方法。那么,抖音怎么带标签引流呢?本文将为您详细解答这个问题,并介绍相关技巧。一、抖音怎么带标签引流?发布视频时,要确保选择与内容相关的标签。这些标签应涵盖视频的主题和关键词,以便让用户通过标签更容易找到您的视频。利用流行标签是增加视频曝光的有效方法。研究当前热门标签和趋势,将其巧妙地融入视频描述和标签中。这些热门标签通常具有更高的曝光度,能够吸引更多观众的关注。3.标签

抖音标签后面的时钟是什么?怎么给抖音账号打标签呢?抖音标签后面的时钟是什么?怎么给抖音账号打标签呢?Mar 24, 2024 pm 03:46 PM

在浏览抖音作品时,我们经常能看到标签后面有一个时钟图标。那么,这个时钟到底是什么呢?本文将围绕“抖音标签后面的时钟是什么”展开讨论,希望为您的抖音使用提供一些有益的参考。一、抖音标签后面的时钟是什么?抖音会推出一些热门话题挑战,用户参与时会在标签后看到一个时钟图标,这代表作品正在参与话题挑战,并显示挑战的剩余时间。对于一些具有时效性的内容,如节假日、特殊活动等,抖音会在标签后面附上时钟图标,提醒用户该内容的有效期限。3.热门标签:当某个标签变得热门时,抖音会在标签后面添加时钟图标,表示这个标签正

html5标签head和header有什么区别html5标签head和header有什么区别Jan 17, 2022 am 11:10 AM

区别:1、head标签用于定义文档头部,它是所有头部元素的容器,而header标签用于定义文档的页眉(介绍信息);2、浏览器都支持head标签,而旧版本浏览器均不支持header标签,需要IE9+以上浏览器才支持header标签。

如何在 Windows 11 中按需使用 OneDrive 的文件如何在 Windows 11 中按需使用 OneDrive 的文件Apr 14, 2023 pm 12:34 PM

&lt;p&gt;Windows 系统上的 OneDrive 应用程序允许您将文件存储在高达 5 GB 的云上。OneDrive 应用程序中还有另一个功能,它允许用户选择一个选项,是将文件保留在系统空间上还是在线提供,而不占用您的系统存储空间。此功能称为按需文件。在这篇文章中,我们进一步探索了此功能,并解释了有关如何在 Windows 11 电脑上的 OneDrive 中按需使用文件的各种选项。&lt;/p&gt;&lt;h2&gt;如何使用 On

深入了解HTML中的video元素深入了解HTML中的video元素Feb 24, 2024 pm 08:18 PM

HTML中video视频标签详解HTML5中的video标签是一种用于在网页上播放视频的标签。它可以使用不同的格式来呈现视频,例如MP4、WebM、Ogg等等。在本篇文章中,我们将详细介绍video标签的使用方法,并提供具体的代码示例。基本结构下面是video标签的基本结构:

钉钉app外部联系人标签怎么删除钉钉app外部联系人标签怎么删除Feb 24, 2024 am 08:20 AM

钉钉app外部联系人标签怎么删除?钉钉中是可以删除外部联系人标签的功能,但是多数小伙伴不知道钉钉外部联系人标签如何的删除,接下来就是小编为用户带来的钉钉app外部联系人标签删除方法图文教程,感兴趣的用户快来一起看看吧!钉钉app外部联系人标签怎么删除1、首先打开钉钉APP,主页面中点击如下图所示的【管理】功能;2、然后进入到企业管理的界面,找到其中的【外部联系人】;3、接着在外部联系人设置功能页,选择【标签管理】服务;4、之后在联系人标签主页面,选择你需要删除的标签组类型;5、最后点击标签组红色

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools