PHP Array sorted Introduction
This article describes the PHP one-dimensional, two-dimensional and multi-dimensional array sorting method.
1, one-dimensional array sorting
a, sort-of this function to sort an array. When this function is the end of the array elements will be from the lowest to the highest re-scheduled. Note: This function in the cell array given new keys. This will remove the original key name rather than just re-order
<? php array ( "lemon" , "orange" , "banana" , "apple" ) ; $ fruits = array ( "lemon", "orange", "banana", "apple"); $fruits ) ; sort ($ fruits); $fruits as $key => $val ) { foreach ($ fruits as $ key => $ val) ( . $key . "] = " . $val . " \n " ; echo "fruits [". $ key. "] =". $ val. "\ n"; ) ? "
The above example will output:
# Fruits [0] = apple
# Fruits [1] = banana
# Fruits [2] = lemon
# Fruits [3] = orange
b, asort - sort of an array and maintain index relations. This function to sort an array, the array index to maintain and associated units. Is mainly used for those units with a very important in order to sort the array.
<? php array ( "d" => "lemon" , "a" => "orange" , "b" => "banana" , "c" => "apple" ) ; $ fruits = array ( "d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"); $fruits ) ; arsort ($ fruits); $fruits as $key => $val ) { foreach ($ fruits as $ key => $ val) ( = $val \n " ; echo "$ key = $ val \ n"; ) ? "
The above example will output:
a = orange
d = lemon
b = banana
c = apple
2, multi-dimensional array sorting
<? php = array ( 'volume' => 67 , 'edition' => 2 ) ; $ data [] = array ( 'volume' => 67, 'edition' => 2); = array ( 'volume' => 86 , 'edition' => 1 ) ; $ data [] = array ( 'volume' => 86, 'edition' => 1); = array ( 'volume' => 85 , 'edition' => 6 ) ; $ data [] = array ( 'volume' => 85, 'edition' => 6); = array ( 'volume' => 98 , 'edition' => 2 ) ; $ data [] = array ( 'volume' => 98, 'edition' => 2); = array ( 'volume' => 86 , 'edition' => 6 ) ; $ data [] = array ( 'volume' => 86, 'edition' => 6); = array ( 'volume' => 67 , 'edition' => 7 ) ; $ data [] = array ( 'volume' => 67, 'edition' => 7); ? "
In this case will in descending order of volume, the edition in ascending order
Now, with an array containing rows, but array_multisort () need to include an array of columns, so use the following code to obtain the column, and then sort
<? php / / Get list of columns $data as $key => $row ) { foreach ($ data as $ key => $ row) ( $key ] = $row [ 'volume' ] ; $ volume [$ key] = $ row [ 'volume']; $key ] = $row [ 'edition' ] ; $ edition [$ key] = $ row [ 'edition']; ) / / The data in descending order according to volume, according to edition ascending order / / Put $ data as the last parameter to generic keys to sort $volume , SORT_DESC , $edition , SORT_ASC , $data ) ; array_multisort ($ volume, SORT_DESC, $ edition, SORT_ASC, $ data); ? "
Collection of data is now sorted, and the results are as follows:
volume | edition
---+---
98 | 2
86 | 1
86 | 6
85 | 6
67 | 2
67 | 7













Recent Comments