PHP array sorting function collection and their linkage analysis _php tutorial

Source: Internet
Author: User
The sorting of several array functions mentioned below has some commonality:
The 1 array is used as the parameter of the sort function, and after sorting, the array itself is changed, and the return value of the function is type bool.
2 A single-a representation of association appears in the function name, meaning that the correspondence of Key=>value remains the same during sorting by value
3 A single k in the function name denotes key, meaning that it is ordered by the value of the array key instead of the value of the array during sorting by values
4 The representation of single r appears in the function name reverse, meaning, in the reverse order of not adding r
5 a single U representation appears in the function name user-defined, meaning, sort using user-defined function, if the logic of the function is parameter 1 <参数2返回负数,则按照升序排列(p1小2返负升)。
--------------------Sort function Ascending order--------------------------------
Copy CodeThe code is as follows:
BOOL Sort (array & $array [, int $sort _flags= sort_regular])
$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--------------------
Copy CodeThe code is as follows:
$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)-----------
Copy CodeThe code is as follows:
$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)---------
Copy CodeThe code is as follows:
$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--------------
Copy CodeThe code is as follows:
$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-----------------------
Copy CodeThe code is as follows:
$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----------------
Copy CodeThe 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 ($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 CodeThe code is as follows:
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" and "4");
Uksort ($a, "CMP");
Var_dump ($a);
?>

Results:
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---------
Copy CodeThe 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);
?>

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---------
Copy CodeThe code is as follows:
$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);
?>

Results:
Array
0 =
Array
0 =
String
' Ten ' (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
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.
Represents the Sort method (the array takes precedence before the value).
==========================================================================================
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:
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 the two-dimensional arrays by the specified key values, or you can specify ascending or descending sorting (the default is ascending), and the 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 ' = ' razor ', ' brand ' = ' Philips ', ' Price ' =>3100),
Array (' name ' = = ' treadmill ', ' brand ' = ' three and ' pine stone ', ' price ' =>4900),
Array (' name ' = ' watch ', ' brand ' = ' casio ', ' 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);

Above is the $array of this two-dimensional array according to ' Price ' from low to high sort.
Output result: (slightly).

http://www.bkjia.com/PHPjc/327874.html www.bkjia.com true http://www.bkjia.com/PHPjc/327874.html techarticle The sorting of several array functions mentioned below has some commonality: The 1 array is used as the parameter of the sort function, and after sorting, the array itself is changed, and the return value of the function is type bool. ...

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