Heim >Backend-Entwicklung >PHP-Tutorial >PHP关联数组的具体使用方法_PHP教程
我们在操作数据库的时候,需要用到下面我们看一个基本的数字格式的数组遍历:
<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关联数组在数据库中的相关使用方法。