首页  >  文章  >  后端开发  >  PHP关联数组的具体使用方法_PHP教程

PHP关联数组的具体使用方法_PHP教程

WBOY
WBOY原创
2016-07-15 13:29:19815浏览

我们在操作数据库的时候,需要用到下面我们看一个基本的数字格式的数组遍历:

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span>   </span></span></li>
<li class=""><span>$temp[0] = "richmond";   </span></li>
<li class="alt"><span>$temp[1] = "tigers";   </span></li>
<li class=""><span>$temp[2] = "premiers";   </span></li>
<li class="alt"><span> </span></li>
<li class="">
<span>for($</span><span class="attribute"><font color="#ff0000">x</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>;$x</span><strong><font color="#006699"><span class="tag"><span class="tag-name">count</span></span></font></strong><span>($temp);$x++)   </span>
</li>
<li class="alt"><span>{   </span></li>
<li class=""><span>echo $temp[$x];   </span></li>
<li class="alt"><span>echo " ";   </span></li>
<li class=""><span>}   </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span>   </span>
</li>
</ol>

然而另外一种更加节省代码的方式是:

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span>   </span></span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">temp</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("richmond", "tigers", "premiers");   </span>
</li>
<li class="alt"><span>foreach ($temp as $element)   </span></li>
<li class=""><span>echo "$element ";   </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span>   </span>
</li>
</ol>

foreach 还能输出文字下标:

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span>   </span></span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">temp</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("club" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "richmond",   </span>
</li>
<li class="alt">
<span>"nickname" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>"tigers",   </span>
</li>
<li class="">
<span>"aim" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "premiers");   </span>
</li>
<li class="alt"><span> </span></li>
<li class="">
<span>foreach ($temp as $</span><span class="attribute"><font color="#ff0000">key</font></span><span> =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> $value)   </span>
</li>
<li class="alt"><span>echo "$key : $value ";   </span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span>   </span>
</li>
</ol>

以上就是PHP关联数组在数据库中的相关使用方法。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446373.htmlTechArticle我们在操作数据库的时候,需要用到 下面我们看一个基本的数字格式的数组遍历: ? php $temp[0]="richmond"; $temp[1]="tigers"; $temp[2]="premiers"; for...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn