Keith
Keith

Reputation: 26499

How to sort object by values in PHP?

I am retrieving a timetable in JSON and it works fine.

I would however like to sort the results by timetable_session->name

First, by Public Swimming Secondly, Indoor Pool and finally Lane Swimming

Any others would come after these etc.

Could someone point me in the right direction?

[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"
    }

Upvotes: 0

Views: 56

Answers (1)

Devon Bessemer
Devon Bessemer

Reputation: 35337

usort allows you to pass a closure defining your own sort algorithm.

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

It looks like you don't want a standard string comparison, so you'll have to define your own algorithm for that but this should get you started.

Upvotes: 2

Related Questions