Sort array of objects by object fields

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

    If you need local based string comparison, you can use strcoll instead of strcmp.

    Remeber to first use setlocale with LC_COLLATE to set locale information if needed.

      usort($your_data,function($a,$b){
        setlocale (LC_COLLATE, 'pl_PL.UTF-8'); // Example of Polish language collation
        return strcoll($a->name,$b->name);
      });
    

提交回复
热议问题