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