PHP array sorting function collection and association analysis between them

Source: Internet
Author: User

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. A single k indicates the key in the function name, meaning that the key is sorted by the array key rather than the value of the array during 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 logic of the function is parameter 1 <parameter 2 returns a negative number, the values are sorted in ascending order (p1 is smaller than 2 and returns to negative rise ).
------------------ Sort the sort function in ascending order --------------------------------
Copy codeThe Code is as follows: bool sort (array & $ array [, int $ sort_flags = SORT_REGULAR])
<? Php
$ 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 --------------------
Copy codeThe Code is as follows: <? Php
$ 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 )-----------
Copy codeThe Code is as follows: <? Php
$ 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 )---------
Copy codeThe Code is as follows: <? Php
$ 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 --------------
Copy codeThe Code is as follows: <? Php
$ 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 -----------------------
Copy codeThe Code is as follows: <? Php
$ 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 ----------------
Copy codeThe Code is as follows: <? Php
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 -----------------
Copy codeThe Code is as follows: <? Php
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 to keep the index relationship unchanged ---------
Copy codeThe Code is as follows: <? Php
// 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 ---------
Copy codeThe Code is as follows: <? Php
$ Ar = array (
Array ("10", 11,100,100, ""),
Array (1, 2, "2", 3, 1)
);

Array_multisort ($ ar [0], SORT_ASC, SORT_STRING,
$ Ar [1], SORT_NUMERIC, SORT_DESC );
Var_dump ($ ar );
?>

Result:
Array
0 =>
Array
0 =>
String
'10' (length = 2)
1 =>
Int
100
2 =>
Int
100
3 =>
Int
11
4 =>
String
'A' (length = 1)
1 =>
Array
0 =>
Int
1
1 =>
Int
3
2 =>
String
'2' (length = 1)
3 =>
Int
2
4 =>
Int
1
// Description:
In the preceding example, the $ ar array is sorted in ascending order based on the string values of $ ar [0]. If the string values are equal, sort the values of the $ ar [1] array in descending order.
2 If the parameter of any position of the array_multisort function is an array, it indicates the value used for sorting,
If multiple array parameters exist, they are sorted first by the array values of the front edge. If they are constants, for example
SORT_ASC, SORT_DESC, SORT_REGULAR, SORT_NUMERIC, SORT_STRING.
Indicates the sorting method (prior to the array value ).
========================================================== ========================================================== ============
PHP two-dimensional array sorting Function
You can use functions such as sort (), asort (), and arsort () to sort PHP one-dimensional arrays. However, you need to customize the sorting of PHP two-dimensional arrays.
The following function sorts a given two-dimensional array by the specified key value. First, let's look at the function definition:Copy codeThe Code is as follows: function array_sort ($ arr, $ keys, $ type = 'asc '){
$ Keysvalue = $ new_array = array ();
Foreach ($ arr as $ k =>$ v ){
$ Keysvalue [$ k] = $ v [$ keys];
}
If ($ type = 'asc '){
Asort ($ keysvalue );
} Else {
Arsort ($ keysvalue );
}
Reset ($ keysvalue );
Foreach ($ keysvalue as $ k => $ v ){
$ New_array [$ k] = $ arr [$ k];
}
Return $ new_array;
}

It can sort two-dimensional arrays by specified key values, or specify the ascending or descending sort method (the default is ascending). Usage example:Copy codeThe Code is as follows: $ array = array (
Array ('name' => 'phone', 'brand' => 'nokia ', 'price' => 1050 ),
Array ('name' => 'laptop ', 'brand' => 'lenovo', 'price' => 4300 ),
Array ('name' => 'shares', 'brand' => 'phillips', 'price' => 3100 ),
Array ('name' => 'treadmill ', 'brand' => 'three', 'price' => 4900 ),
Array ('name' => 'Watch ', 'brand' => 'case', 'price' => 960 ),
Array ('name' => 'LCD TV', 'brand' => 'sony ', 'price' => 6299 ),
Array ('name' => 'laser printer ', 'brand' => 'hp', 'price' => 1200)
);
$ ShoppingList = array_sort ($ array, 'price ');
Print_r ($ ShoppingList );

The above orders the two-dimensional array $ array in descending order of 'price.
Output result: (omitted ).

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.