如何在PHP中按值对对象排序?

时间:2018-07-20 20:46:37

标签: php json sorting

我正在使用JSON检索时间表,并且工作正常。

但是我想按timetable_session-> name排序结果

首先,通过公共游泳 其次,室内游泳池 最后泳道

在这些之后,还会有其他人来

有人可以指出我正确的方向吗?

[1]=>
    object(stdClass)#1924 (11) {
      ["id"]=>
      int(2569543)
      ["start_time"]=>
      string(5) "06:00"
      ["end_time"]=>
      string(5) "07:15"
      ["facility_name"]=>
      string(13) "Swimming Pool"
      ["date"]=>
      string(10) "2018-07-20"
      ["day"]=>
      string(6) "Friday"
      ["term_type"]=>
      object(stdClass)#1925 (2) {
        ["id"]=>
        int(1)
        ["name"]=>
        string(6) "Normal"
      }
      ["is_cancelled"]=>
      bool(false)
      ["timetable_session"]=>
      object(stdClass)#1923 (2) {
        ["id"]=>
        int(35531)
        ["name"]=>
        string(13) "Lane Swimming"
      }
      ["facility"]=>
      object(stdClass)#1922 (4) {
        ["id"]=>
        int(70912)
        ["length"]=>
        float(25)
        ["primary_name"]=>
        string(13) "Swimming Pool"
        ["facility_type"]=>
        object(stdClass)#1916 (2) {
          ["id"]=>
          int(31)
          ["name"]=>
          string(11) "Indoor Pool"
        }
      }
      ["lanes"]=>
      string(1) "3"
    }
    [2]=>
    object(stdClass)#1919 (11) {
      ["id"]=>
      int(2569529)
      ["start_time"]=>
      string(5) "06:00"
      ["end_time"]=>
      string(5) "10:30"
      ["facility_name"]=>
      string(13) "Swimming Pool"
      ["date"]=>
      string(10) "2018-07-20"
      ["day"]=>
      string(6) "Friday"
      ["term_type"]=>
      object(stdClass)#1920 (2) {
        ["id"]=>
        int(1)
        ["name"]=>
        string(6) "Normal"
      }
      ["is_cancelled"]=>
      bool(false)
      ["timetable_session"]=>
      object(stdClass)#1918 (2) {
        ["id"]=>
        int(35541)
        ["name"]=>
        string(15) "Public Swimming"
      }
      ["facility"]=>
      object(stdClass)#1917 (4) {
        ["id"]=>
        int(70912)
        ["length"]=>
        float(25)
        ["primary_name"]=>
        string(13) "Swimming Pool"
        ["facility_type"]=>
        object(stdClass)#1911 (2) {
          ["id"]=>
          int(31)
          ["name"]=>
          string(11) "Indoor Pool"
        }
      }
      ["lanes"]=>
      string(1) "3"
    }
    [3]=>
    object(stdClass)#1914 (11) {
      ["id"]=>
      int(2569536)
      ["start_time"]=>
      string(5) "07:15"
      ["end_time"]=>
      string(5) "08:00"
      ["facility_name"]=>
      string(13) "Swimming Pool"
      ["date"]=>
      string(10) "2018-07-20"
      ["day"]=>
      string(6) "Friday"
      ["term_type"]=>
      object(stdClass)#1915 (2) {
        ["id"]=>
        int(1)
        ["name"]=>
        string(6) "Normal"
      }
      ["is_cancelled"]=>
      bool(false)
      ["timetable_session"]=>
      object(stdClass)#1913 (2) {
        ["id"]=>
        int(35581)
        ["name"]=>
        string(9) "Swimfit®"
      }
      ["facility"]=>
      object(stdClass)#1912 (4) {
        ["id"]=>
        int(70912)
        ["length"]=>
        float(25)
        ["primary_name"]=>
        string(13) "Swimming Pool"
        ["facility_type"]=>
        object(stdClass)#1906 (2) {
          ["id"]=>
          int(31)
          ["name"]=>
          string(11) "Indoor Pool"
        }
      }
      ["lanes"]=>
      string(1) "1"
    }

1 个答案:

答案 0 :(得分:2)

usort允许您传递定义您自己的排序算法的闭包。

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

您似乎不需要标准的字符串比较,因此您必须为此定义自己的算法,但这应该会让您入门。