PHP two-dimensional array sorting simple implementation method, php two-dimensional array sorting. PHP two-dimensional array sorting simple implementation method, php two-dimensional array sorting this article describes the PHP two-dimensional array sorting simple implementation method. For your reference, please refer to the following: simple implementation of func PHP two-dimensional array sorting, and php two-dimensional array sorting.
This article describes how to sort two-dimensional arrays in PHP. We will share this with you for your reference. The details are as follows:
Function multi_compare ($ a, $ B) {$ val_arr = array ('gold' => 'asc ', 'silver '=> 'desc' // You can also add additional sorting conditions); 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 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 ))