PHP multidimensional array sorting (usort,uasort) _php tips

Source: Internet
Author: User
Tags sorts
Array of numeric indices:
BOOL Usort (array & $array, callback $cmp _function)
The Usort function sorts the specified array (parameter 1) by the specified method (parameter 2).
When we want to sort a multidimensional array, each element of a multidimensional array is an array type, and how do two arrays compare size? This is required for user customization (whether the first element of each array is compared or ...).

Copy Code code as follows:

<?php
Defining multidimensional arrays
$a = Array (
Array ("Sky", "Blue"),
Array ("Apple", "Red"),
Array ("Tree", "green"));
A custom array comparison function that is compared by the second element of the array.
function My_compare ($a, $b) {
if ($a [1] < $b [1])
return-1;
else if ($a [1] = = $b [1])
return 0;
Else
return 1;
}
Sort
Usort ($a, ' my_compare ');
Output results
foreach ($a as $elem) {
echo "$elem [0]: $elem [1]<br/>];
}

?>

The results are:
Copy Code code as follows:

Sky:blue
Tree:green
Apple:red

Associative arrays:
BOOL Uasort (array & $array, callback $cmp _function)
BOOL Uksort (array & $array, callback $cmp _function)

Uasort, the Uksort usage is the same as Usort, where Uasort () sorts the value (value) of the associative array, and uksort () sorts the keyword (key) of the associative array.
Copy Code code as follows:

<?php
$a = Array (
' Sunday ' => array (0, ' 7th '),
' Friday ' => array (5, ' 5th '),
' Tuesday ' => array (2, ' 2nd '));

function My_compare ($a, $b) {
if ($a [1] < $b [1])
return-1;
else if ($a [1] = = $b [1])
return 0;
Else
return 1;
}
Sort by the second element (7th,5th,2nd) of the value of the $a array
Uasort ($a, ' my_compare ');
foreach ($a as $key => $value) {
echo "$key: $value [0] $value [1]<br/>];
}
Sort by the second character (r,u,u) of the keyword in the $a array
Uksort ($a, ' my_compare ');
foreach ($a as $key => $value) {
echo "$key: $value [0] $value [1]<br/>];
}

?>

The results are:

Tuesday:2 2nd
Friday:5 5th
sunday:0 7th
Friday:5 5th
sunday:0 7th
Tuesday:2 2nd

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.