Explanation of the array sorting function in PHP

Source: Internet
Author: User

A large number of array sorting functions are introduced in php. Next we will introduce the usage of php array sorting.

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

The Code is as follows: Copy code
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 --------------------

The Code is as follows: Copy code
<? 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 )-----------

The Code is as follows: Copy code
<? 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 'banana '(length = 6)
'D' => string 'limon' (length = 5)
'A' => string 'Orange '(length = 6)

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

The Code is as follows: Copy code

<? 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 'banana '(length = 6)
'C' => string 'apple' (length = 5)

------------------ Ksort is sorted in ascending order by the array key ------------ <? 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 'banana '(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: Copy code

<? 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 'banana '(length = 6)
'A' => string 'Orange '(length = 6)


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

The Code is as follows: Copy code

<? 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 -----------------

The Code is as follows: Copy code

<? 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 ---------

The Code is as follows: Copy code

<? 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 ---------

The Code is as follows: Copy code

<? 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 ).

 


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.