PHP array sorting various methods summary _php tutorial

Source: Internet
Author: User
In the PHP array sorting function has a lot of (rsort,sort,asort,rsort,krsort,ksort,array_multisort), let me give my friends to summarize some of our commonly used array sorting function and the user to write an array of sort instances, You can refer to a friend who needs to use it.

The most commonly used function of array sorting is sort ($arr); It is arranged in ascending order by the key value of the array, and the sorted array key name is no longer the original key name, and is the key name that is reset by the new array.
And sometimes we ask for more complicated sorting. If the key name is sorted, the Ksort ($arr) is used here; function, which is sorted according to the key name of the array and maintains the original key-value relationship. The corresponding asort ($arr); function, which is the ordering of key values and maintaining the original key-value relationship.

Same principle, rsort (); Arsort (); Krsort (); Functions except sorting are sorted in descending order, others with sort (); Rsort (); Ksort (); Same.
Array manipulation is a very important basis for PHP, and I hope to use it well.


Sort key values
That is, the size of the code value by identifier Ascⅱ order.

Ksort (): arranged in array identifier order
Krsort (): sorted by array identifier
Example 1:

The code is as follows Copy Code

$languages =array (
' C ' = ' php ',
' d ' = ' asp ',
' A ' = ' jsp ',
' B ' = ' java '
);
Krsort ($languages);
foreach ($languages as $key = = $val) {
echo "$key = $val". '
';
};
?>

Sort by element value

Asort (): Sorts the array in order from small to large;
Rsort (): Sorts the array in reverse order from large to small.

Change the 8-11 line code for instance 1 to:

The code is as follows Copy Code

Asort ($languages);
Print_r ($languages);
echo "
";
Rsort ($languages);
Print_r ($languages);


Delete the original key name sort

Sort (): Sorts the array in order from small to large;
Rsort (): Sorts the array in reverse order from large to small.

Change the 8-11 line code for instance 2 to:

The code is as follows Copy Code

Sort ($languages);
foreach ($languages as $key = = $val) {
echo "languages[$key] = $val". '
';
};

Array_multisort-Sort multiple arrays or multidimensional arrays
Description
BOOL Array_multisort (array ar1 [, mixed arg [, mixed ... [, array ...]] )

Example 1. To sort multiple arrays

The code is as follows Copy Code

$ar 1 = Array ("Ten", "N", "a");
$ar 2 = Array (1, 3, "2", 1);
Array_multisort ($ar 1, $ar 2);

Var_dump ($ar 1);
Var_dump ($ar 2);
?>

In this example, after sorting, the first array will contain "ten", "a", 100,100. The second array will contain 1, 1, "2", 3. The order of the items in the second array is exactly the same as the corresponding items (100 and 100) in the first array.

The code is as follows Copy Code

Array (4) {
[0]=> string (2) "10"
[1]=> string (1) "a"
[2]=> Int (100)
[3]=> Int (100)
}
Array (4) {
[0]=> Int (1)
[1]=> Int (1)
[2]=> string (1) "2"
[3]=> Int (3)
}

Example 2. To sort a multidimensional array

The code is as follows Copy Code

$ar = Array (Array ("Ten", "+", "a"), Array (1, 3, "2", 1));
Array_multisort ($ar [0], SORT_ASC, sort_string,
$ar [1], sort_numeric, SORT_DESC);
?>

In this example, after sorting, the first array will contain 10,100,100, "a" (ascending sort as a string), and the second array will contain 1, 3, "2", and 1 (as a descending sort of a number).


PHP array Sorting functions are many, but also very powerful
such as: Arsort,asort,krsort,ksort,sort ...
But sometimes it's not enough to meet our needs.
If there is a two-dimensional array, we want to sort by a value in a two-dimensional array
You need to use the Usort. Custom array Ordering


Examples are as follows:

code as follows copy code



$AA = Array ("Score" =>60),
Array ("Score" =>70),
Array ("Score" =>80),
Array ("Score" =>90),
Array ("Score" =>20),
Array ("Score" =>10),
Array ("Score" =>50),
Array ("score" =>30));

Echo '------pre-sort output------
';
Var_dump ($AA); Pre-sort output

Usort ($AA, "CMP"); Sort processing (from large to small)

Echo '
------output------After sorting
';
Var_dump ($AA); Sort out output

/**
* Custom Sorting criteria
* @param array $a
* @param array $b
* @return BOOL
*/
function cmp ($a, $b) {
if ($a ["score"] = = $b ["Score"]) {
return 0;
}
return ($a ["Score"] < $b ["score"])? 1:-1;
}

?>

Output Result:

code as follows copy code
< p>------sorted before output------
Array (8) {[0]=> Array (1) {["Score"]=> int ()}
[1]=> Array (1) {["Score"]=> Int ( ()}
[2]=> Array (1) {["score"]=> int)}
[3]=> Array (1) {["Score"]=> int (+)}
[4]=> Array (1) {["Score"]=> int ()}
[5]=> Array (1) {["Score"]=> Int (Ten)}
[6]=> Array (1) {["Score"]=> int
[7]=> Array (1) {["Score"]=> int ()}}
------sorted after output------
Array (8) {[0]=> Array (1) {["Score"] = = Int (+)}
[1]=> Array (1) {["score"]=> int)}
[2]=> Array (1) {["Score"]=> int (+)}
[3]= > Array (1) {["Score"]=> int]}
[4]=> Array (1) {["Score"]=> int ()}
[5]=> Array (1) {["Score" ]=> int (+)}
[6]=> Array (1) {["Score"]=> int ()}
[7]=> Array (1) {["Score"]=> Int (Ten)}}

td>

http://www.bkjia.com/PHPjc/631541.html www.bkjia.com true http://www.bkjia.com/PHPjc/631541.html techarticle in the PHP array sorting function has a lot of (rsort,sort,asort,rsort,krsort,ksort,array_multisort), below I come to you to summarize some of our commonly used array sorting function with the user ...

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