A collection of PHP array sorting functions and their relationship analysis _php techniques

Source: Internet
Author: User
Tags array sort function definition
The ordering 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 bool type.
2 function name appears single A association, meaning, in the process of ordering by value, maintain the key=>value of the corresponding relationship is unchanged
3 function name in a single K represents key, meaning, in the process of sorting by value according to the array key instead of the value of the array sort
4 The expression reverse of a single r in the name of a function, in the opposite order of not adding r
In the 5 function name, a single U representation user-defined, meaning, is sorted using a user-defined function, and if the logic of the function is the argument 1< parameter 2 returns a negative number, it is sorted in ascending order (P1 small 2 to negative liters).
--------------------sort function in ascending order--------------------------------
Copy Code code as follows:

BOOL Sort (array & $array [, int $sort _flags= sort_regular])
<?php
$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 Descending order--------------------
Copy Code code as follows:

<?php
$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 in ascending order of two-dimensional array values (keep Key=>value associated)-----------
Copy Code code as follows:

<?php
$fruits = Array ("D" => "Lemon", "a" => "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 (keep Key=>value associated)---------
Copy Code code as follows:

<?php
$fruits = Array ("D" => "Lemon", "a" => "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 Code code as follows:

<?php
$fruits = Array ("D" => "Lemon", "a" => "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 array key-----------------------
Copy Code code as follows:

<?php
$fruits = Array ("D" => "Lemon", "a" => "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 Code code as follows:

<?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 ($a);
?>

Results:
Array
0 =>
Int
1
1 =>
Int
2
2 =>
Int
3
3 =>
Int
5
4 =>
Int
6
-----------------Uksort uses custom functions to sort by the key of the array-----------------
Copy Code code as follows:

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

Results:
Array
' An apple ' =>
Int
3
' A banana ' =>
Int
4
' The earth ' =>
Int
2
' John ' =>
Int
1
--------------Uasort The array with the custom function to sort by value, keeping the index relationship unchanged---------
Copy Code code as follows:

<?php
Comparison function
function cmp ($a, $b) {
if ($a = = $b) {
return 0;
}
Return ($a < $b)? -1:1;
}
Array to is 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 Code code as follows:

<?php
$ar = Array (
Array ("A", "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 example above: $ar array precedence follows the string value of $ar[0] in ascending order, if the string values are equal, and then sorted by the numeric values in the $ar[1] array.
2 The parameter of any position of the Array_multisort function, if it is an array, represents the value used for the sort.
If you have more than one array parameter, precedence is sorted by the preceding array value, if it is a constant, for example
SORT_ASC, Sort_desc, Sort_regular,sort_numeric, sort_string.
Represents a sort method (precedence before array values).
==========================================================================================
php Two-dimensional array sorting function
A PHP one-dimensional array can be sorted using sort (), Asort (), Arsort (), but the sort of PHP two-dimensional array needs to be customized.
The following function is to sort a given two-dimensional array by the specified key value, first look at the function definition:
Copy Code code 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 a two-dimensional array by the specified key value, or you can specify ascending or descending sort (default is ascending), using the example:
Copy Code code as follows:

$array = Array (
Array (' name ' => ' cell 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);

The above is a sort of $array this two-dimensional array in terms of ' price ' from low to high.
Output result: (slightly).

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.