PHP array sorting function usage

Source: Internet
Author: User
Tags arrays function definition sorts

Php array sorting functions include:

Sort (& $ arr [, fruits]) sorts arrays in ascending order, and assigns a new key name to return bool
Rsort (& $ arr [, fruits]) sorts the array in reverse order and assigns a new key name.
Asort (& $ arr [, fruits]) sorts arrays and keeps indexes unchanged.
Arsort (& $ arr [, fruits]) sorts arrays in reverse order and keeps indexes unchanged.


Ksort (& $ arr [, fruits]) sorts arrays by key name.
Krsort (& $ arr [, fruits]) sorts the number of groups by key name in reverse order.


Natsort (& $ arr) sorts the array key values by 'natural sorted' and letters by length.
Natcasesort (& $ arr) performs a case-insensitive 'natural ranking' on the array'


Usort (& $ arr, cmp_function) user-defined function sorts an array and resorts the key names
Uksort (& $ arr, cmp_function) user-defined function sorts an array by key name
Uasort (& $ arr, cmp_function) user-defined function sorts the array and keeps the index unchanged


Array_multisort ($ arr, mixed)

The second parameter indicates that the sorting behavior can be changed based on the value.


SORT_REGULAR normal comparison unit SORT_NUMERIC unit is compared as a number
The SORT_STRING unit is used as a string to compare SROT_LOCALE_STRING. Based on the current local settings, the unit is used as a string for comparison.


------------------ Sort the sort function in ascending order --------------------------------
Bool sort (array & $ array [, int $ sort_flags = SORT_REGULAR])

The code is as follows: Copy code

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

'Bana' (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

'Bana' (length = 6)
'C' =>

String

'Apple' (length = 5)

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

The code is as follows: Copy code

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

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

'Bana' (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.


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: Copy code

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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.