PHP二維數組排序 array_multisort

不言
發布: 2023-03-24 06:22:02
原創
3093 人瀏覽過

這篇文章主要介紹的內容是關於PHP二維數組排序array_multisort,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

對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 
   
    function my_sort($arrays,$sort_key,$sort_order=SORT_ASC,$sort_type=SORT_NUMERIC ){  
        if(is_array($arrays)){  
            foreach ($arrays as $array){  
                if(is_array($array)){  
                    $key_arrays[] = $array[$sort_key];  
                }else{  
                    return false;  
                }  
            }  
        }else{  
            return false;  
        } 
        array_multisort($key_arrays,$sort_order,$sort_type,$arrays);  
        return $arrays;  
    } 
   
    $person =  array( 
                    array(&#39;id&#39;=>1,&#39;name&#39;=>&#39;fj&#39;,&#39;weight&#39;=>100,&#39;height&#39;=>180), 
                    array(&#39;id&#39;=>2,&#39;name&#39;=>&#39;tom&#39;,&#39;weight&#39;=>53,&#39;height&#39;=>150), 
                    array(&#39;id&#39;=>3,&#39;name&#39;=>&#39;jerry&#39;,&#39;weight&#39;=>120,&#39;height&#39;=>156), 
                    array(&#39;id&#39;=>4,&#39;name&#39;=>&#39;bill&#39;,&#39;weight&#39;=>110,&#39;height&#39;=>190), 
                    array(&#39;id&#39;=>5,&#39;name&#39;=>&#39;linken&#39;,&#39;weight&#39;=>80,&#39;height&#39;=>200), 
                    array(&#39;id&#39;=>6,&#39;name&#39;=>&#39;madana&#39;,&#39;weight&#39;=>95,&#39;height&#39;=>110), 
                    array(&#39;id&#39;=>7,&#39;name&#39;=>&#39;jordan&#39;,&#39;weight&#39;=>70,&#39;height&#39;=>170) 
                ); 
       
    var_dump($person); 
       
    $person = my_sort($person,&#39;name&#39;,SORT_ASC,SORT_STRING); 
   
    var_dump($person); 
       
    $person = my_sort($person,&#39;weight&#39;); 
   
    var_dump($person); 
?>
登入後複製

結果如下:

array (size=7)
  0 =>
    array (size=4)
      &#39;id&#39; => int 1
      &#39;name&#39; => string &#39;fj&#39; (length=2)
      &#39;weight&#39; => int 100
      &#39;height&#39; => int 180
  1 =>
    array (size=4)
      &#39;id&#39; => int 2
      &#39;name&#39; => string &#39;tom&#39; (length=3)
      &#39;weight&#39; => int 53
      &#39;height&#39; => int 150
  2 =>
    array (size=4)
      &#39;id&#39; => int 3
      &#39;name&#39; => string &#39;jerry&#39; (length=5)
      &#39;weight&#39; => int 120
      &#39;height&#39; => int 156
  3 =>
    array (size=4)
      &#39;id&#39; => int 4
      &#39;name&#39; => string &#39;bill&#39; (length=4)
      &#39;weight&#39; => int 110
      &#39;height&#39; => int 190
  4 =>
    array (size=4)
      &#39;id&#39; => int 5
      &#39;name&#39; => string &#39;linken&#39; (length=6)
      &#39;weight&#39; => int 80
      &#39;height&#39; => int 200
  5 =>
    array (size=4)
      &#39;id&#39; => int 6
      &#39;name&#39; => string &#39;madana&#39; (length=6)
      &#39;weight&#39; => int 95
      &#39;height&#39; => int 110
  6 =>
    array (size=4)
      &#39;id&#39; => int 7
      &#39;name&#39; => string &#39;jordan&#39; (length=6)
      &#39;weight&#39; => int 70
      &#39;height&#39; => int 170
array (size=7)
  0 =>
    array (size=4)
      &#39;id&#39; => int 4
      &#39;name&#39; => string &#39;bill&#39; (length=4)
      &#39;weight&#39; => int 110
      &#39;height&#39; => int 190
  1 =>
    array (size=4)
      &#39;id&#39; => int 1
      &#39;name&#39; => string &#39;fj&#39; (length=2)
      &#39;weight&#39; => int 100
      &#39;height&#39; => int 180
  2 =>
    array (size=4)
      &#39;id&#39; => int 3
      &#39;name&#39; => string &#39;jerry&#39; (length=5)
      &#39;weight&#39; => int 120
      &#39;height&#39; => int 156
  3 =>
    array (size=4)
      &#39;id&#39; => int 7
      &#39;name&#39; => string &#39;jordan&#39; (length=6)
      &#39;weight&#39; => int 70
      &#39;height&#39; => int 170
  4 =>
    array (size=4)
      &#39;id&#39; => int 5
      &#39;name&#39; => string &#39;linken&#39; (length=6)
      &#39;weight&#39; => int 80
      &#39;height&#39; => int 200
  5 =>
    array (size=4)
      &#39;id&#39; => int 6
      &#39;name&#39; => string &#39;madana&#39; (length=6)
      &#39;weight&#39; => int 95
      &#39;height&#39; => int 110
  6 =>
    array (size=4)
      &#39;id&#39; => int 2
      &#39;name&#39; => string &#39;tom&#39; (length=3)
      &#39;weight&#39; => int 53
      &#39;height&#39; => int 150
array (size=7)
  0 =>
    array (size=4)
      &#39;id&#39; => int 2
      &#39;name&#39; => string &#39;tom&#39; (length=3)
      &#39;weight&#39; => int 53
      &#39;height&#39; => int 150
  1 =>
    array (size=4)
      &#39;id&#39; => int 7
      &#39;name&#39; => string &#39;jordan&#39; (length=6)
      &#39;weight&#39; => int 70
      &#39;height&#39; => int 170
  2 =>
    array (size=4)
      &#39;id&#39; => int 5
      &#39;name&#39; => string &#39;linken&#39; (length=6)
      &#39;weight&#39; => int 80
      &#39;height&#39; => int 200
  3 =>
    array (size=4)
      &#39;id&#39; => int 6
      &#39;name&#39; => string &#39;madana&#39; (length=6)
      &#39;weight&#39; => int 95
      &#39;height&#39; => int 110
  4 =>
    array (size=4)
      &#39;id&#39; => int 1
      &#39;name&#39; => string &#39;fj&#39; (length=2)
      &#39;weight&#39; => int 100
      &#39;height&#39; => int 180
  5 =>
    array (size=4)
      &#39;id&#39; => int 4
      &#39;name&#39; => string &#39;bill&#39; (length=4)
      &#39;weight&#39; => int 110
      &#39;height&#39; => int 190
  6 =>
    array (size=4)
      &#39;id&#39; => int 3
      &#39;name&#39; => string &#39;jerry&#39; (length=5)
      &#39;weight&#39; => int 120
      &#39;height&#39; => int 156
登入後複製

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

轉自:https://www.cnblogs.com/tdalcn/p/6420055.html

相關推薦:

PHP二維數組切割為字串並去除重複的值

php二維數組中如何對其中一維數組中的某個字段進行排序

#

以上是PHP二維數組排序 array_multisort的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!