0

I have an api query done in php (Laravel) from a Wild Apricot member management system and it is returning an array of events that has the following structure (see below). I would like to sort the array by StartDate in ascending order to prep the array display that is done and passed to the Laravel selector widget. Wondering what the best approach to do this would be... any ideas and/or thoughts would be welcomed! Will chug along and test, but thought I would post that up here regardless. ;-P

{
  "Events": [
    {
      "Id": 4077234,
      "Url": "https://api.wildapricot.org/v2.2/accounts/ACCOUNTID/Events/4077234",
      "EventType": "Regular",
      "StartDate": "2021-12-03T19:00:00+11:00",
      "EndDate": "2021-12-05T15:30:00+11:00",
      "Location": "LOCATION1",
      "RegistrationEnabled": true,
      "RegistrationsLimit": 1,
      "PendingRegistrationsCount": 0,
      "ConfirmedRegistrationsCount": 1,
      "CheckedInAttendeesNumber": 0,
      "Tags": [],
      "AccessLevel": "Restricted",
      "StartTimeSpecified": true,
      "EndTimeSpecified": true,
      "HasEnabledRegistrationTypes": true,
      "Name": "NAME1"
    },
    {
      "Id": 4334234,
      "Url": "https://api.wildapricot.org/v2.2/accounts/ACCOUNTID/Events/4334234",
      "EventType": "Regular",
      "StartDate": "2021-11-27T00:00:00+11:00",
      "EndDate": "2021-11-28T00:00:00+11:00",
      "Location": "LOCATION2",
      "RegistrationEnabled": true,
      "RegistrationsLimit": 50,
      "PendingRegistrationsCount": 0,
      "ConfirmedRegistrationsCount": 9,
      "CheckedInAttendeesNumber": 0,
      "Tags": [],
      "AccessLevel": "Restricted",
      "StartTimeSpecified": false,
      "EndTimeSpecified": false,
      "HasEnabledRegistrationTypes": true,
      "Name": "NAME2"
    },
}

Here's the query that is being done..

        $queryParams3 = array(
            '$async' => 'false', 
            // We want the list of events
            // https://api.wildapricot.org/v2.2/accounts/ACCOUNTID/Events?$filter=(StartDate gt 2021-05-01) 
            '$filter' => "(StartDate gt 2021-05-01)"
        ); 
        $url3 = $accountUrl . '/Events/?' . http_build_query($queryParams3);
        $eventsResult = $waApiClient->makeRequest($url3); // json is returned > have been done a json_decode on it.. 
        $events =  $eventsResult['Events'];

Reason of wanting to do this is that it just makes it simpler for the user to read the events listed in events that are current (e.g. closest to today's date) vs events that are further out.. as background info, this is for a small sms portal that I am building out. ;-P

VK3FNOR
  • 13
  • 1
  • 6
  • 2
    Does this answer your question? [Sort array of objects by object fields](https://stackoverflow.com/questions/4282413/sort-array-of-objects-by-object-fields) – Kinglish Jun 05 '21 at 00:34

2 Answers2

0

Laravel has a built-in way for managing sets of data with Collections.

https://laravel.com/docs/8.x/collections

// ...
$events = $eventsResult['Events'];

$sorted_events = collect($events)->sortBy('StartDate');
matticustard
  • 4,850
  • 1
  • 13
  • 18
0

The native PHP solution uses usort(). Only $arr["Events"] has to be passed as first parameter. The spaceship operator is used for the comparison in the user function.

$json = '{
  "Events": [
    {
      "Id": 4077234,
   :
}';

Create an array from json:

$arr = json_decode($json, true);

Sort by "StartDate" ASC:

usort($arr["Events"],function($a,$b){return $a["StartDate"] <=> $b["StartDate"];});
jspit
  • 7,276
  • 1
  • 9
  • 17