-1

我有一个由对象组成的数组。

[
    {
        "name" => "First",
        "order" => 5
    },
    {
        "name" => "Second",
        "order" => 3
    },
    {
        "name" => "Third",
        "order" => 9
]

如何通过升序“订单”值来订购它?

我应该得到:

[
    {
        "name" => "Second",
        "order" => 3
    },
    {
        "name" => "First",
        "order" => 5
    },
    {
        "name" => "Third",
        "order" => 9
];
4

1 回答 1

2

您应该能够将这样的 PHPusort()函数用于数组:

usort($array, function($a, $b){return $a["order"]-$b["order"];});

或对象:

usort($array, function($a, $b){return $a->order-$b->order;});

这是 PHP 参考:usort()

于 2013-06-28T10:07:43.540 回答