Home > Backend Development > PHP Tutorial > How to use associative arrays in PHP_PHP tutorial

How to use associative arrays in PHP_PHP tutorial

WBOY
Release: 2016-07-15 13:29:19
Original
839 people have browsed it

When we operate the database, we need to use Let’s look at a basic array traversal in digital format:

<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 class=""><SPAN>$temp[0] = "richmond";   </SPAN><LI class=alt><SPAN>$temp[1] = "tigers";   </SPAN><LI class=""><SPAN>$temp[2] = "premiers";   </SPAN><LI class=alt><SPAN> </SPAN><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><SPAN class=tag-name>count</SPAN></FONT></STRONG><SPAN>($temp);$x++)   </SPAN></SPAN><LI class=alt><SPAN>{   </SPAN><LI class=""><SPAN>echo $temp[$x];   </SPAN><LI class=alt><SPAN>echo " ";   </SPAN><LI class=""><SPAN>}   </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span>   </span>
</li></ol>
Copy after login

However, another way to save code is:

<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 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></SPAN><LI class=alt><SPAN>foreach ($temp as $element)   </SPAN><LI class=""><SPAN>echo "$element ";   </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span>   </span>
</li></ol>
Copy after login

foreach can also output text subscripts:

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

The above is the relevant usage of PHP associative arrays in the database.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446373.htmlTechArticleWhen we operate the database, we need to use the following. Let’s look at a basic digital format array traversal: ? php $temp[0]="richmond"; $temp[1]="tigers"; $temp[2]="premiers"; for...
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