Maison  >  Article  >  développement back-end  >  案例分析PHP数组查询_PHP教程

案例分析PHP数组查询_PHP教程

WBOY
WBOYoriginal
2016-07-15 13:27:11813parcourir

学习PHP数组时,你可能会遇到问题,这里将介绍PHP数组查询,在这里拿出来和大家分享一下。PHP4.0中共有超过30个新的数组相关函数。其中很多通用函数允许你检查给定数组中是否存在特定对象、对数组元素计数、增加或删除元素,或对元素排序。

如果你有很大的一个数组,而所要完成的仅是找出一个存在的给定值,你可以使用in_array()以返回true 或 false。如下代码将输出“Not found in this array”——因为你将在$namesArray中寻找一个并不存在的“Alber ”。你可以对任何数组添加元素,无论是在已存在数组的开始或末尾。你也可以使用函数以创建一个包含两个或多个数组元素的新数组。合并时每个数组将按需要的顺序排列。如果你的数组已经有内部的排序,你需要对新的合并数组重排序。

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>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446541.htmlTechArticle学习PHP数组时,你可能会遇到问题,这里将介绍PHP数组查询,在这里拿出来和大家分享一下。PHP4.0中共有超过30个新的数组相关函数。其中...
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn