案例分析PHP數(shù)組查詢
學(xué)習(xí)PHP數(shù)組時(shí),你可能會(huì)遇到問題,這里將介紹PHP數(shù)組查詢,在這里拿出來(lái)和大家分享一下。PHP4.0中共有超過30個(gè)新的數(shù)組相關(guān)函數(shù)。其中很多通用函數(shù)允許你檢查給定數(shù)組中是否存在特定對(duì)象、對(duì)數(shù)組元素計(jì)數(shù)、增加或刪除元素,或?qū)υ嘏判颉?
#T#如果你有很大的一個(gè)數(shù)組,而所要完成的僅是找出一個(gè)存在的給定值,你可以使用in_array()以返回true 或 false。如下代碼將輸出“Not found in this array”——因?yàn)槟銓⒃?namesArray中尋找一個(gè)并不存在的“Alber ”。你可以對(duì)任何數(shù)組添加元素,無(wú)論是在已存在數(shù)組的開始或末尾。你也可以使用函數(shù)以創(chuàng)建一個(gè)包含兩個(gè)或多個(gè)數(shù)組元素的新數(shù)組。合并時(shí)每個(gè)數(shù)組將按需要的順序排列。如果你的數(shù)組已經(jīng)有內(nèi)部的排序,你需要對(duì)新的合并數(shù)組重排序。
PHP數(shù)組查詢代碼:
- /**
 - *倒序數(shù)組
 - *@paramaInputarray要排序的數(shù)組
 - *@param$bystring排序?qū)ο?key為按數(shù)組下標(biāo)排序,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要冒溝的數(shù)組
 - *@param$bystring排序?qū)ο?key為按數(shù)組下標(biāo)排序,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'));
 















 
 
 

 
 
 
 