データベースを操作するときは、デジタル形式での基本的な配列トラバーサルを見てみましょう:
<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>
ただし、コードを保存する別の方法は次のとおりです:
<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はテキスト添字を出力することもできます:
<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>
上記は、データベースでの PHP 連想配列の使用方法に関連するものです。