Maison > Article > développement back-end > 案例分析PHP数组查询_PHP教程
学习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> </li> <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> </li> <li class=""><span>print_r($tmMax); </span></li> <li class="alt"><span>list($k,$v)=each($tmMax); </span></li> <li class=""><span>$res[$k]=$v; </span></li> <li class="alt"><span>} </span></li> <li class=""><span>return$res; </span></li> <li class="alt"><span>} </span></li> <li class=""><span> </span></li> <li class="alt"><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>functiongetMax(&$aInput,$by){ </span></li> <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> </li> <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> </li> <li class="alt"> <span>if($</span><span class="attribute"><font color="#ff0000">by</font></span><span>=='key') </span> </li> <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> </li> <li class="alt"><span>else </span></li> <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> </li> <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 class="tag-name">xmp</span><span class="tag">></span></span></font></strong><span>'; </span> </li> <li class="alt"><span>print_r(aryReverts($list,'value')); </span></li> </ol>