我有一个由对象组成的数组。
[
{
"name" => "First",
"order" => 5
},
{
"name" => "Second",
"order" => 3
},
{
"name" => "Third",
"order" => 9
]
如何通过提升“订单”值来订购?
我应该得到:
[
{
"name" => "Second",
"order" => 3
},
{
"name" => "First",
"order" => 5
},
{
"name" => "Third",
"order" => 9
];
答案 0 :(得分:2)
你应该可以像这样使用PHP usort()
函数来表示数组:
usort($array, function($a, $b){return $a["order"]-$b["order"];});
或对象:
usort($array, function($a, $b){return $a->order-$b->order;});
以下是PHP参考:usort()