我有一组数组,需要在php中按升序还是降序对数组进行排序?
Array
(
[0] => Array
(
[product_name] => Blips
[product_url] =>
[product_id] => 123
[state] => GA
)
[1] => Array
(
[product_name] => Alpha
[product_id] => 586
[state] => GA
)
)
答案 0 :(得分:0)
您可以使用usort和strnatcmp来实现:
usort($myArray, function($a, $b) {
return strnatcmp($a['product_name'], $b['product_name']);
});
如果您希望它按降序排序,则只需在对数组排序后将其反转:
$myArray = array_reverse($myArray);