PHP array sorting function How to use _php tutorial

Source: Internet
Author: User
Tags array sort sorts
There are many data sorting functions in PHP, including one-dimensional array sorting function and two-dimensional array ordering function, including simple sort function ascending sort, rsort descending order, etc.

The PHP array sort function has

Sort (& $arr [, fruits]) sorts the array from low to high and assigns a new key name back to BOOL
Rsort (& $arr [, fruits]) reverse sort the array and give the new key name
Asort (& $arr [, fruits]) sorts the array and keeps the index intact
Arsort (& $arr [, fruits]) reverse-sort the array and keep the index intact


Ksort (& $arr [, fruits]) sorts the array by key name
Krsort (& $arr [, fruits]) reverse-sort the number of groups by key name


Natsort (& $arr) ' Natural sorting ' of array key values by alphabetical order of length
Natcasesort (& $arr) An array of case-insensitive ' natural rankings '


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


Array_multisort ($arr, mixed)

The second parameter is the ability to change the sort behavior by value


Sort_regular Normal comparison unit sort_numeric unit is compared as a number
The sort_string unit is compared as a string srot_locale_string the cell as a string based on the current local setting.


--------------------sort function Ascending order--------------------------------
BOOL Sort (array & $array [, int $sort _flags= sort_regular])

The code is as follows Copy Code

$fruits = Array ("Lemon", "orange", "banana", "apple");
Sort ($fruits);
Var_dump ($fruits);
?>
Results:
Array

0 =

String

' Apple ' (length=5)
1 =

String

' Banana ' (length=6)
2 =

String

' Lemon ' (length=5)
3 =

String

' Orange ' (length=6)

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

The code is as follows Copy Code

$fruits = Array ("Lemon", "orange", "banana", "apple");
Rsort ($fruits);
Var_dump ($fruits);
?>
Results:
Array
0 =

String

' Orange ' (length=6)
1 =

String

' Lemon ' (length=5)
2 =

String

' Banana ' (length=6)
3 =

String

' Apple ' (length=5)

---------------Asort is arranged in ascending order of two-dimensional array values (maintains Key=>value association)-----------

The code is as follows Copy Code

$fruits = Array ("d" = "lemon", "a" and "Orange", "b" = "banana", "c" = "apple");
Asort ($fruits);
Var_dump ($fruits);
?>
Results:
Array
' C ' =

String

' Apple ' (length=5)
' B ' + =

String

' Banana ' (length=6)
' d ' =

String

' Lemon ' (length=5)
' A ' + =

String

' Orange ' (length=6)

--------------Arsort in descending order of two-dimensional array values (maintains Key=>value association)--------------

The code is as follows Copy Code

$fruits = Array ("d" = "lemon", "a" and "Orange", "b" = "banana", "c" = "apple");
Arsort ($fruits);
Var_dump ($fruits);
?>
Results
Array
' A ' + =

String

' Orange ' (length=6)
' d ' =

String

' Lemon ' (length=5)
' B ' + =

String

' Banana ' (length=6)
' C ' =

String

' Apple ' (length=5)

--------------------Ksort in ascending order of the array's key--------------

The code is as follows Copy Code

$fruits = Array ("d" = "lemon", "a" and "Orange", "b" = "banana", "c" = "apple");
Ksort ($fruits);
Var_dump ($fruits);
?>
Results
Array

' A ' + =

String

' Orange ' (length=6)
' B ' + =

String

' Banana ' (length=6)
' C ' =

String

' Apple ' (length=5)
' d ' =

String

' Lemon ' (length=5)

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

The code is as follows Copy Code

$fruits = Array ("d" = "lemon", "a" and "Orange", "b" = "banana", "c" = "apple");
Krsort ($fruits);
Var_dump ($fruits);
?>

Array
' d ' =

String

' Lemon ' (length=5)
' C ' =

String

' Apple ' (length=5)
' B ' + =

String

' Banana ' (length=6)
' A ' + =

String

' Orange ' (length=6)


----------------Usort functions are sorted by user-defined functions----------------

The code is as follows Copy Code

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 ($a);
?>
Results:
Array
0 =

Int

1
1 =

Int

2
2 =

Int

3
3 =

Int

5
4 =

Int

6

-----------------Uksort Use a custom function to sort by the key of the array-----------------

copy code

"!--? PHP
function cmp ($a, $b)
{
$a = Preg_replace (' @^ (a|an|the) @ ', ' ', $a);
$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 ($a);
?
Result:
Array
' an apple ' =>

int

3
' a banana ' =>

int

4
' the Earth ' =>

int

2
' John ' =>

int

1

-------------------Uasort the array with a custom function to sort by value, keeping the index relationship constant---------

The code is as follows Copy Code

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);
?>
Results:
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 to sort multiple arrays or multidimensional arrays---------

code as follows copy code

"!--? php
$ar = array (
Array ( "Ten", one, one, one, "a"),
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

' Ten ' (length=2)
1 =>

int


2 =>

int

4
3 =>

int

One
=>

String

' a ' (length=1)
1 =
Array
0 =>

int

1
1 =>

int

3
2 =>

String

' 2 ' (length=1)
3 =>

Int

2
4 =>

int

1

Description
1 in the above example: $ar array precedence follows the string value of $ar[0] in ascending order, if the string value is equal, and then in descending order of the numeric value of the $ar[1] array.
2 The parameter of any position of the Array_multisort function if it is an array, indicating the value to use when sorting,
If there are multiple array parameters, prioritize the array values in front of them, if they are constants, for example
SORT_ASC, Sort_desc, Sort_regular,sort_numeric, sort_string.


PHP two-dimensional array sorting function

A PHP one-dimensional array can be sorted with functions such as sort (), Asort (), Arsort (), but the ordering of PHP's two-dimensional arrays needs to be customized.

The following function is to sort a given two-dimensional array according to the specified key value, and first 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;
}

http://www.bkjia.com/PHPjc/632806.html www.bkjia.com true http://www.bkjia.com/PHPjc/632806.html techarticle There are many data sorting functions in PHP, including one-dimensional array ordering function and two-dimensional array ordering function, including the simple sort function ascending sort, rsort descending permutation and other PHP array sorting functions have ...

  • Related Article

    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.