php二维数据排序 array_multisort

作者: dreamfly 分类: php 发布时间: 2017-08-25 09:05

对2维数组或者多维数组排序是常见的问题,在PHP中我们有个专门的多维数组排序函数,下面简单介绍下:

array_multisort(array1,sorting order, sorting type,array2,array3..)是对多个数组或多维数组进行排序的函数。

array1 必需。规定输入的数组。
sorting order 可选。规定排列顺序。可能的值是 SORT_ASC 和 SORT_DESC。
sorting type 可选。规定排序类型。可能的值是SORT_REGULAR、SORT_NUMERIC和SORT_STRING。
array2 可选。规定输入的数组。
array3 可选。规定输入的数组。


参数中的数组被当成一个表的列并以行来进行排序 - 这类似 SQL 的 ORDER BY 子句的功能。第一个数组是要排序的主要数组。数组中的行(值)比较为相同的话,就会按照下一个输入数组中相应值的大小进行排序,依此类推。

第一个参数是数组,随后的每一个参数可能是数组,也可能是下面的排序顺序标志(排序标志用于更改默认的排列顺序)之一:

  • SORT_ASC - 默认,按升序排列。(A-Z)

  • SORT_DESC - 按降序排列。(Z-A)

随后可以指定排序的类型:

  • SORT_REGULAR - 默认。将每一项按常规顺序排列。

  • SORT_NUMERIC - 将每一项按数字顺序排列。

  • SORT_STRING - 将每一项按字母顺序排列

[php] view plain copy

  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. ?>   

结果如下:

[php] view plain copy

  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开发者给我们提供了更好的办法,我们就可以省去不必要的麻烦了。

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!