PHP 配列を学習するときに問題が発生する可能性があります。ここでは PHP 配列クエリを紹介し、それをここで共有します。 PHP4.0 には、30 を超える配列関連の新しい関数が追加されています。これらの一般的な関数の多くを使用すると、特定のオブジェクトが指定された配列に存在するかどうかを確認したり、配列要素をカウントしたり、要素を追加または削除したり、要素を並べ替えたりすることができます。
PHP 配列クエリ コード:
<ol class="dp-xml"> <li class="alt"><span><span>/** </span></span></li> <li class=""><span>*倒序数组 </span></li> <li class="alt"><span>*@paramaInputarray要排序的数组 </span></li> <li class=""><span>*@param$bystring排序对象,key为按数组下标排序,value为按值排序 </span></li> <li class="alt"><span>*/ </span></li> <li class=""> <span>functionaryReverts($aInput,$</span><span class="attribute"><font color="#ff0000">by</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">'key'</font></span><span>){ </span> </li> <li class="alt"><span>if(!is_array($aInput)){ </span></li> <li class=""><span>returnfalse; </span></li> <li class="alt"><span>} </span></li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">res</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>(); </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">n</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">count</font></span><span>($aInput); </span> </li> <li class=""> <span>for($</span><span class="attribute"><font color="#ff0000">i</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>;$i</span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN>$n;$i++){ </SPAN></SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>tmMax</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>getMax</FONT></SPAN><SPAN>($aInput,$by); </SPAN></SPAN><LI class=""><SPAN>print_r($tmMax); </SPAN><LI class=alt><SPAN>list($k,$v)=each($tmMax); </SPAN><LI class=""><SPAN>$res[$k]=$v; </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN>return$res; </SPAN><LI class=alt><SPAN>} </SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>/** </SPAN><LI class=alt><SPAN>*@paramaInputarray要冒沟的数组 </SPAN><LI class=""><SPAN>*@param$bystring排序对象,key为按数组下标排序,value为按值排序 </SPAN><LI class=alt><SPAN>*/ </SPAN><LI class=""><SPAN>functiongetMax(&$aInput,$by){ </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>max</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>null</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>maxKey</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>null</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=alt><SPAN>if($</SPAN><SPAN class=attribute><FONT color=#ff0000>by</FONT></SPAN><SPAN>=='key') </SPAN></SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>tar</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>'k'</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=alt><SPAN>else </SPAN><LI class=""><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>tar</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>'v'</FONT></SPAN><SPAN>; </SPAN></SPAN><LI class=alt><SPAN>foreach($aInputas$</SPAN><SPAN class=attribute><FONT color=#ff0000>k</FONT></SPAN><SPAN>=</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span>$v){ </span> </li> <li class=""> <span>if($$tar</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$max){ </span> </li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">max</font></span><span>=$$tar; </span> </li> <li class=""> <span>$</span><span class="attribute"><font color="#ff0000">maxKey</font></span><span>=$k; </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">maxValue</font></span><span>=$aInput[$maxKey]; </span> </li> <li class=""><span>unset($aInput[$maxKey]); </span></li> <li class="alt"> <span>returnarray($</span><span class="attribute"><font color="#ff0000">maxKey</font></span><span>=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$maxValue); </span> </li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span> </span></li> <li class="alt"> <span>$</span><span class="attribute"><font color="#ff0000">list</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>('apple'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>300,'orange'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>150,'banana'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>100,'mango'=</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>330); </span> </li> <li class=""> <span>echo'</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>xmp</SPAN><SPAN class=tag>></span></font></strong><span>'; </span> </li> <li class="alt"><span>print_r(aryReverts($list,'value')); </span></li> </ol>