This article mainly introduces the method of sorting two-dimensional arrays in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.
The code is 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);
The running result is 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 ) )
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
php method to obtain the check box value and a simple example
php methods and examples to implement process control switch
php method of using text to count visits, graphic and text explanation
The above is the detailed content of How to sort two-dimensional array in PHP. For more information, please follow other related articles on the PHP Chinese website!