-1

I have an array composed of objects.

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

How can I order it by ascending "order" value ?

I should get :

[
    {
        "name" => "Second",
        "order" => 3
    },
    {
        "name" => "First",
        "order" => 5
    },
    {
        "name" => "Third",
        "order" => 9
];
Lukmo
  • 1,688
  • 5
  • 20
  • 31
  • possible duplicate of [Sort multidimensional Array by Value (2)](http://stackoverflow.com/questions/2699086/sort-multidimensional-array-by-value-2) – deceze Jun 28 '13 at 10:03
  • 1
    Please see [Reference: all basic ways to sort arrays in PHP](http://stackoverflow.com/q/17364127/476) – deceze Jun 28 '13 at 12:48

1 Answers1

2

You should be able to use the PHP usort() function like this for arrays:

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

or for objects:

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

Here's the PHP reference: usort()