Sort array of objects by object fields

后端 未结 19 1879
情书的邮戳
情书的邮戳 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:21

    if you're using php oop you might need to change to:

    public static function cmp($a, $b) 
    {
        return strcmp($a->name, $b->name);
    }
    
    //in this case FUNCTION_NAME would be cmp
    usort($your_data, array('YOUR_CLASS_NAME','FUNCTION_NAME')); 
    

提交回复
热议问题