PHP array sorting function collection and association analysis between them

Source: Internet
Author: User
This article provides a detailed analysis of the collection of PHP array sorting functions and their relationships. For more information, see

This article provides a detailed analysis of the collection of PHP array sorting functions and their relationships. For more information, see

The sorting of several array functions mentioned below has some commonalities:
1. The array is used as a parameter of the sorting function. After sorting, the array itself changes. The return value of the function is of the bool type.
2. a In the function name indicates association. It means that the key => value is retained during value sorting.
3. In the function name, a single k indicates the key and virtual host. It means that the function is sorted by the array key rather than the array value in the value sorting process.
4. the reverse of a single r is displayed in the function name, which means that the reverse is arranged in the reverse order of not adding r.
5. the user-defined expression of single u appears in the function name, which means that the user-defined function is used for sorting. If the function logic is parameter 1 <参数2返回负数,则按照升序排列(p1小2返负升)。
------------------ Sort the sort function in ascending order --------------------------------

The Code is as follows:


Bool sort (array & $ array [, int $ sort_flags = SORT_REGULAR])
$ Fruits = array ("lemon", "orange", "banana", "apple ");
Sort ($ fruits );
Var_dump ($ fruits );
?>
Result:
Array
0 =>
String
'Apple' (length = 5)
1 =>
String
'Bana' (length = 6)
2 =>
String
'Limon' (length = 5)
3 =>
String
'Orange '(length = 6)


------------------ Rsort sort in descending order --------------------

The Code is as follows:


$ Fruits = array ("lemon", "orange", "banana", "apple ");
Rsort ($ fruits );
Var_dump ($ fruits );
?>
Result:
Array
0 =>
String
'Orange '(length = 6)
1 =>
String
'Limon' (length = 5)
2 =>
String
'Bana' (length = 6)
3 =>
String
'Apple' (length = 5)


--------------- Asort is arranged in ascending order of two-dimensional array values (maintain the relationship between key => value )-----------

The Code is as follows:


$ Fruits = array ("d" => "lemon", "a" => "orange", "B" => "banana ", "c" => "apple ");
Asort ($ fruits );
Var_dump ($ fruits );
?>
Result:
Array
'C' =>
String
'Apple' (length = 5)
'B' =>
String
'Bana' (length = 6)
'D' =>
String
'Limon' (length = 5)
'A' =>
String
'Orange '(length = 6)


--------- Arsort is sorted in descending order of two-dimensional array values (maintain the relationship between key => value )---------

The Code is as follows:


$ Fruits = array ("d" => "lemon", "a" => "orange", "B" => "banana ", "c" => "apple ");
Arsort ($ fruits );
Var_dump ($ fruits );
?>
Result
Array
'A' =>
String
'Orange '(length = 6)
'D' =>
String
'Limon' (length = 5)
'B' =>
String
'Bana' (length = 6)
'C' =>
String
'Apple' (length = 5)


------------------ Ksort is sorted in ascending order by the array key --------------

The Code is as follows:


$ Fruits = array ("d" => "lemon", "a" => "orange", "B" => "banana ", "c" => "apple ");
Ksort ($ fruits );
Var_dump ($ fruits );
?>
Result
Array
'A' =>
String
'Orange '(length = 6)
'B' =>
String
'Bana' (length = 6)
'C' =>
String
'Apple' (length = 5)
'D' =>
String
'Limon' (length = 5)


------------------- Krsort is sorted in descending order of the array key -----------------------

The Code is as follows:


$ Fruits = array ("d" => "lemon", "a" => "orange", "B" => "banana ", "c" => "apple ");
Krsort ($ fruits );
Var_dump ($ fruits );
?>
Array
'D' =>
String
'Limon' (length = 5)
'C' =>
String
'Apple' (length = 5)
'B' =>
String
'Bana' (length = 6)
'A' =>
String
'Orange '(length = 6)


---------------- Sort functions by user-defined functions ----------------

The Code is as follows:


Function cmp ($ a, $ B)
{
If ($ a = $ B ){
Return 0;
}
Return ($ a <$ B )? -1: 1;
}
$ A = array (3, 2, 5, 6, 1 );
Usort ($ a, "cmp ");
Var_dump ($ );
?>


Result:
Array
0 =>
Int
1
1 =>
Int
2
2 =>
Int
3
3 =>
Int
5
4 =>
Int
6
----------------- Uksort uses a custom function to sort the keys of the array -----------------

The Code is as follows:


Function cmp ($ a, $ B)
{
$ A = preg_replace ('@ ^ (a | an | the) @', '', $ );
$ B = preg_replace ('@ ^ (a | an | the) @', '', $ B );
Return strcasecmp ($ a, $ B );
}
$ A = array ("John" => 1, "the Earth" => 2, "an apple" => 3, "a banana" => 4 );
Uksort ($ a, "cmp ");
Var_dump ($ );
?>


Result:
Array
'An apple' =>
Int
3
'A banana '=>
Int
4
The Earth =>
Int
2
'John' =>
Int
1
-------------- Uasort sorts the array by value using a custom function, and keeps the index relationship unchanged ---------

The Code is as follows:


// Comparison function
Function cmp ($ a, $ B ){
If ($ a = $ B ){
Return 0;
}
Return ($ a <$ B )? -1: 1;
}
// Array to be sorted
$ Array = array ('A' => 4, 'B' => 8, 'c' =>-1, 'D' =>-9, 'E' => 2, 'F' => 5, 'G' => 3, 'H' =>-4 );
Var_dump ($ array );
// Sort and print the resulting array
Uasort ($ array, 'cmp ');
Var_dump ($ array );
?>


Result:
Array
'A' =>
Int
4
'B' =>
Int
8
'C' =>
Int
-1
'D' =>
Int
-9
'E' =>
Int
2
'F' =>
Int
5
'G' =>
Int
3
'H' =>
Int
-4
Array
'D' =>
Int
-9
'H' =>
Int
-4
'C' =>
Int
-1
'E' =>
Int
2
'G' =>
Int
3
'A' =>
Int
4
'F' =>
Int
5
'B' =>
Int
8
----------------- Array_multisort sorts multiple arrays or multi-dimensional arrays ---------

The Code is as follows:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.