Home >Backend Development >PHP Tutorial >How to use associative arrays in PHP_PHP tutorial
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>
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>
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>
The above is the relevant usage of PHP associative arrays in the database.