In this paper, we describe a simple method to realize php two-dimensional array sorting. Share to everyone for your reference, specific as follows:
function Multi_compare ($a, $b)
{
$val _arr = Array (
' Gold ' => ' ASC ',
' silver ' => ' desc '// Additional sorting criteria can also be added
);
foreach ($val _arr as $key => $val) {
if ($a [$key] = = $b [$key]) {
continue;
}
Return (($val = = ' desc ') -1:1) * (($a [$key] < $b [$key]) -1:1);
return 0;
}
$arr = Array (
' Gold ' =>1, ' Silver ' =>2),
array (' Gold ' =>8, ' Silver ' =>10),
Array (' Gold ' =>8, ' Silver ' =>8),
array (' Gold ' =>2, ' Silver ' =>1),
);
Uasort ($arr, ' multi_compare ');
Print_r ($arr);
The results of the operation are as follows:
Array
(
[0] => array
(
[gold] => 1
[silver] => 2
)
[3] => array
(
[ Gold] => 2
[silver] => 1
)
[1] => Array
(
[gold] => 8
[silver] =>
)
[2] => Array
(
[Gold] => 8
[silver] => 8
)
)
More about PHP Interested readers can view the site topics: "PHP Sorting algorithm Summary", "PHP basic Grammar Introductory Course", "PHP error and Exception handling method summary" and "PHP common functions and Skills summary"
I hope this article will help you with the PHP program design.