Sort array of objects by object fields

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

    This is what I have for a utility class

    class Util
    {
        public static function sortArrayByName(&$arrayToSort, $meta) {
            usort($arrayToSort, function($a, $b) use ($meta) {
                return strcmp($a[$meta], $b[$meta]);
            });
        }
    }
    

    Call it:

    Util::sortArrayByName($array, "array_property_name");
    

提交回复
热议问题