A simple implementation method for PHP two-dimensional array sorting, PHP two-dimensional array sorting
The example in this article describes a simple implementation method for PHP two-dimensional array sorting. Share it with everyone for your reference, the details are as follows:
function multi_compare($a, $b)
{
$val_arr = array(
'gold'=>'asc',
'silver'=>'desc'//还可以增加额外的排序条件
);
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(
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);
Copy after login
The running results are as follows:
Array
(
[0] => Array
(
[gold] => 1
[silver] => 2
)
[3] => Array
(
[gold] => 2
[silver] => 1
)
[1] => Array
(
[gold] => 8
[silver] => 10
)
[2] => Array
(
[gold] => 8
[silver] => 8
)
)
Copy after login
Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP Sorting Algorithm", "Introduction Tutorial on PHP Basic Syntax", "Summary of PHP Error and Exception Handling Methods" and "Common PHP Functions and Techniques" Summary》
I hope this article will be helpful to everyone in PHP programming.
Articles you may be interested in:
- Detailed explanation of the method of sorting two-dimensional arrays in php
- The method of array_multisort in php to sort multi-dimensional arrays
- php Usage examples of the dedicated array sorting class ArraySortUtil
- php method of sorting arrays through the sort() function
- PHP implementation of multi-dimensional array sorting function sharing for specified fields
- php selection sort method Implementation of array sorting example analysis
- PHP insertion sort method implementation of array sorting example
- PHP array sorting usort, uksort and sort function usage
- PHP array sorting sort, asort and ksort Usage examples
- PHP example sharing of two-dimensional array sorting
- 3 methods of PHP two-dimensional array sorting and custom functions shared
- A PHP two-dimensional array sorting function Share
- php two-dimensional array sorting method (array_multisort usort)
- php two-dimensional array sorting detailed explanation
http://www.bkjia.com/PHPjc/1099062.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1099062.htmlTechArticleA simple implementation method of PHP two-dimensional array sorting, php two-dimensional array sorting This article describes the simple implementation of PHP two-dimensional array sorting Implementation method. Share it with everyone for your reference, the details are as follows: func...