Sort array of objects by object fields

后端 未结 19 1589
情书的邮戳
情书的邮戳 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 03:10

    usort($array, 'my_sort_function');
    
    var_dump($array);
    
    function my_sort_function($a, $b)
    {
        return $a->name < $b->name;
    }
    

    The same code will be with the count field.

    More details about usort: http://ru2.php.net/usort

    Btw, where did you get that array from? I hope that not from database?

提交回复
热议问题