国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php 二維數組排序使用詳解

2024-09-04 11:46:37
字體:
來源:轉載
供稿:網友

昨天要排序數組的時候發現了,要按時間排序,但是php并沒有內設這個函數,所以在網上找到了這個代碼,第一個參數為數組,第二個是要排序的元素,第三個為排序方式,下面就是php 二維數組排序的代碼:

  1. function arraySort($arr$keys$type = 'asc') { 
  2.          $keysvalue = $new_array = array(); 
  3.          foreach ($arr as $k => $v){ 
  4.              $keysvalue[$k] = $v[$keys]; 
  5.          } 
  6.          $type == 'asc' ? asort($keysvalue) : arsort($keysvalue); 
  7.          reset($keysvalue); 
  8.          foreach ($keysvalue as $k => $v) { 
  9.             $new_array[$k] = $arr[$k]; 
  10.          } 
  11.          return $new_array
  12.      } 
  13.  
  14. $arr[] = array("name"=>"1","time"=>1) ; 

array_multisort(array1,sorting order, sorting type,array2,array3..)是對多個數組或多維數組進行排序的函數。

  1. <?php   
  2.    
  3.     function my_sort($arrays,$sort_key,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC ){    
  4.         if(is_array($arrays)){    
  5.             foreach ($arrays as $array){    
  6.                 if(is_array($array)){    
  7.                     $key_arrays[] = $array[$sort_key];    
  8.                 }else{    
  9.                     return false;    
  10.                 }    
  11.             }    
  12.         }else{    
  13.             return false;    
  14.         }   
  15.         array_multisort($key_arrays,$sort_order,$sort_type,$arrays);    
  16.         return $arrays;    
  17.     }   
  18.    
  19.     $person =  array(   
  20.                     array('id'=>1,'name'=>'fj','weight'=>100,'height'=>180),   
  21.                     array('id'=>2,'name'=>'tom','weight'=>53,'height'=>150),   
  22.                     array('id'=>3,'name'=>'jerry','weight'=>120,'height'=>156),   
  23.                     array('id'=>4,'name'=>'bill','weight'=>110,'height'=>190),   
  24.                     array('id'=>5,'name'=>'linken','weight'=>80,'height'=>200),   
  25.                     array('id'=>6,'name'=>'madana','weight'=>95,'height'=>110),   
  26.                     array('id'=>7,'name'=>'jordan','weight'=>70,'height'=>170)   
  27.                 );   
  28.        
  29.     var_dump($person);   
  30.        
  31.     $person = my_sort($person,'name',SORT_ASC,SORT_STRING);   
  32.    
  33.     var_dump($person);   
  34.        
  35.     $person = my_sort($person,'weight');   
  36.    
  37.     var_dump($person);   
  38. ?> 

結果如下:

  1.  array (size=7) 
  2.   0 =>  
  3.     array (size=4) 
  4.       'id' => int 1 
  5.       'name' => string 'fj' (length=2) 
  6.       'weight' => int 100 
  7.       'height' => int 180 
  8.   1 =>  
  9.     array (size=4) 
  10.       'id' => int 2 
  11.       'name' => string 'tom' (length=3) 
  12.       'weight' => int 53 
  13.       'height' => int 150 
  14.   2 =>  
  15.     array (size=4) 
  16.       'id' => int 3 
  17.       'name' => string 'jerry' (length=5) 
  18.       'weight' => int 120 
  19.       'height' => int 156 
  20.   3 =>  
  21.     array (size=4) 
  22.       'id' => int 4 
  23.       'name' => string 'bill' (length=4) 
  24.       'weight' => int 110 
  25.       'height' => int 190 
  26.   4 =>  
  27.     array (size=4) 
  28.       'id' => int 5 
  29.       'name' => string 'linken' (length=6) 
  30.       'weight' => int 80 
  31.       'height' => int 200 
  32.   5 =>  
  33.     array (size=4) 
  34.       'id' => int 6 
  35.       'name' => string 'madana' (length=6) 
  36.       'weight' => int 95 
  37.       'height' => int 110 
  38.   6 =>  
  39.     array (size=4) 
  40.       'id' => int 7 
  41.       'name' => string 'jordan' (length=6) 
  42.       'weight' => int 70 
  43.       'height' => int 170 
  44. array (size=7) 
  45.   0 =>  
  46.     array (size=4) 
  47.       'id' => int 4 
  48.       'name' => string 'bill' (length=4) 
  49.       'weight' => int 110 
  50.       'height' => int 190 
  51.   1 =>  
  52.     array (size=4) 
  53.       'id' => int 1 
  54.       'name' => string 'fj' (length=2) 
  55.       'weight' => int 100 
  56.       'height' => int 180 
  57.   2 =>  
  58.     array (size=4) 
  59.       'id' => int 3 
  60.       'name' => string 'jerry' (length=5) 
  61.       'weight' => int 120 
  62.       'height' => int 156 
  63.   3 =>  
  64.     array (size=4) 
  65.       'id' => int 7 
  66.       'name' => string 'jordan' (length=6) 
  67.       'weight' => int 70 
  68.       'height' => int 170 
  69.   4 =>  
  70.     array (size=4) 
  71.       'id' => int 5 
  72.       'name' => string 'linken' (length=6) 
  73.       'weight' => int 80 
  74.       'height' => int 200 
  75.   5 =>  
  76.     array (size=4) 
  77.       'id' => int 6 
  78.       'name' => string 'madana' (length=6) 
  79.       'weight' => int 95 
  80.       'height' => int 110 
  81.   6 =>  
  82.     array (size=4) 
  83.       'id' => int 2 
  84.       'name' => string 'tom' (length=3) 
  85.       'weight' => int 53 
  86.       'height' => int 150 
  87. array (size=7) 
  88.   0 =>  
  89.     array (size=4) 
  90.       'id' => int 2 
  91.       'name' => string 'tom' (length=3) 
  92.       'weight' => int 53 
  93.       'height' => int 150 
  94.   1 =>  
  95.     array (size=4) 
  96.       'id' => int 7 
  97.       'name' => string 'jordan' (length=6) 
  98.       'weight' => int 70 
  99.       'height' => int 170 
  100.   2 =>  
  101.     array (size=4) 
  102.       'id' => int 5 
  103.       'name' => string 'linken' (length=6) 
  104.       'weight' => int 80 
  105.       'height' => int 200 
  106.   3 =>  
  107.     array (size=4) 
  108.       'id' => int 6 
  109.       'name' => string 'madana' (length=6) 
  110.       'weight' => int 95 
  111.       'height' => int 110 
  112.   4 =>  
  113.     array (size=4) 
  114.       'id' => int 1 
  115.       'name' => string 'fj' (length=2) 
  116.       'weight' => int 100 
  117.       'height' => int 180 
  118.   5 =>  
  119.     array (size=4) 
  120.       'id' => int 4 
  121.       'name' => string 'bill' (length=4) 
  122.       'weight' => int 110 
  123.       'height' => int 190 
  124.   6 =>  
  125.     array (size=4) 
  126.       'id' => int 3 
  127.       'name' => string 'jerry' (length=5) 
  128.       'weight' => int 120 
  129.       'height' => int 156 

這里的重點就是,先把要排序的key存到一個一維數組中,然后就可以使用array_multisort()這個函數,將數組按照key進行排序了,當然,這里的排序你完全可以不適用array_multisort()這個函數,僅僅通過foreach遍歷也能達到這個效果,但是既然php開發者給我們提供了更好的辦法,我們就可以省去不必要的麻煩了。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 海阳市| 息烽县| 开阳县| 大厂| 莱州市| 广宗县| 平阴县| 玛纳斯县| 湟源县| 河源市| 邹平县| 德江县| 喀喇| 和田县| 清水河县| 肇庆市| 嫩江县| 萍乡市| 海伦市| 会泽县| 菏泽市| 黄陵县| 常德市| 平泉县| 杂多县| 万安县| 广安市| 密云县| 淮安市| 莱芜市| 广水市| 新巴尔虎左旗| 水富县| 隆昌县| 赫章县| 婺源县| 钦州市| 迭部县| 通化县| 潼南县| 定兴县|