Sort array of objects by object fields

后端 未结 19 1555
情书的邮戳
情书的邮戳 2020-11-22 02:28

How can I sort this array of objects by one of its fields, like name or count ?

  Array
(
    [0] => stdClass Object
        (
          


        
19条回答
  •  抹茶落季
    2020-11-22 02:57

    Heres a nicer way using closures

    usort($your_data, function($a, $b)
    {
        return strcmp($a->name, $b->name);
    });
    

    Please note this is not in PHP's documentation but if you using 5.3+ closures are supported where callable arguments can be provided.

提交回复
热议问题