Sort PHP 2D Array in Reverse Order
To sort a PHP 2D array in descending order, you can use the array_multisort() function to achieve this. Below is an example code:
// 假设有一个二维数组
$myArray = array(
array("name" => "John", "age" => 30),
array("name" => "Jane", "age" => 25),
array("name" => "Mark", "age" => 35)
);
// 将数组按照age字段进行降序排列
usort($myArray, function($a, $b) {
return $b['age'] - $a['age'];
});
// 打印排序后的数组
print_r($myArray);
In the example above, we used the usort() function with an anonymous function to sort the two-dimensional array $myArray in descending order based on the age field. Finally, we print out the sorted array.