PHP array sorting function collection and association analysis between them _ php skills

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 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 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 to keep 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:


$ 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:

The 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:

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