PHP multi-dimensional array sorting (usort, uasort) _ PHP Tutorial

Source: Internet
Author: User
PHP multi-dimensional array sorting (usort, uasort ). Numeric index array: boolusort (arraylt ;? Php defines multidimensional arrays $ aarray (array (sky, blue), array (apple, red), array (tree, green); custom array comparison function, numeric index array:
Bool usort (array & $ array, callback $ cmp_function)
The usort function sorts the specified array (parameter 1) in the specified way (parameter 2.
When we want to sort multi-dimensional arrays, each element of the multi-dimensional array is an array type. how can we compare the sizes of the two arrays? This is user-defined (compare by the first element of each array or ...).

The code is as follows:


// Define multi-dimensional arrays
$ A = array (
Array ("sky", "blue "),
Array ("apple", "red "),
Array ("tree", "green "));
// Custom array comparison function, which 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 result
Foreach ($ a as $ elem ){
Echo "$ elem [0]: $ elem [1]
";
}

?>


Result:

The code is as follows:


Sky: blue
Tree: green
Apple: red


Join array:
Bool uasort (array & $ array, callback $ cmp_function)
Bool uksort (array & $ array, callback $ cmp_function)

The usage of uasort and uksort is the same as that of usort. uasort () sorts the values of associated arrays, and uksort () sorts the keywords (keys) of associated arrays.

The code is as follows:


$ 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 $ a array
Uasort ($ a, 'My _ compare ');
Foreach ($ a as $ key => $ value ){
Echo "$ key: $ value [0] $ value [1]
";
}
// Sort by the second character (r, u, u) of the keyword of $ a array
Uksort ($ a, 'My _ compare ');
Foreach ($ a as $ key => $ value ){
Echo "$ key: $ value [0] $ value [1]
";
}

?>


Result:

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

Optional bool usort (array lt ;? Php // defines multi-dimensional arrays $ a = array ("sky", "blue"), array ("apple", "red"), array ("tree ", "green"); // custom array comparison function ,...

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.