0

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Devon, I'll check out usort.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.