案例分析PHP数组查询
学习PHP数组时,你可能会遇到问题,这里将介绍PHP数组查询,在这里拿出来和大家分享一下。PHP4.0中共有超过30个新的数组相关函数。其中很多通用函数允许你检查给定数组中是否存在特定对象、对数组元素计数、增加或删除元素,或对元素排序。
PHP数组查询代码:
- /**
- *倒序数组
- *@paramaInputarray要排序的数组
- *@param$bystring排序对象,key为按数组下标排序,value为按值排序
- */
- functionaryReverts($aInput,$by=key){
- if(!is_array($aInput)){
- returnfalse;
- }
- $res=array();
- $n=count($aInput);
- for($i=0;$i<$n;$i++){
- $tmMax=getMax($aInput,$by);
- print_r($tmMax);
- list($k,$v)=each($tmMax);
- $res[$k]=$v;
- }
- return$res;
- }
- /**
- *@paramaInputarray要冒沟的数组
- *@param$bystring排序对象,key为按数组下标排序,value为按值排序
- */
- functiongetMax(&$aInput,$by){
- $max=null;
- $maxKey=null;
- if($by==key)
- $tar=k;
- else
- $tar=v;
- foreach($aInputas$k=>$v){
- if($$tar>$max){
- $max=$$tar;
- $maxKey=$k;
- }
- }
- $maxValue=$aInput[$maxKey];
- unset($aInput[$maxKey]);
- returnarray($maxKey=>$maxValue);
- }
- $list=array(apple=>300,orange=>150,banana=>100,mango=>330);
- echo<xmp>;
- print_r(aryReverts($list,value));
永久链接:http://www.phprm.com/frame/php1003432.html
转载随意!带上文章地址吧。
- 上一篇: 挖掘PHP数组函数库array数组创建和使用
- 下一篇: 关于PHP数组遍历对比测试