0

I'm trying to sort the "matches" arrays from a Challonge JSON so that the matches with the "suggested_play_order" from lowest to highest are listed. Any help is appreciated. So far I know that a JSON is seen as a string so you can't use usort or ksort unless you use json_decode. I want to decode it, change the order of the matches based on the "suggested play order" from lowest to highest. (unless it can be done without decoding it) then reencoding it to JSON in the correct order.

Json Example

{
"match": {
  "id": 214256829,
  "tournament_id": 8868290,
  "state": "complete",
  "player1_id": 131117246,
  "player2_id": 131142374,
  "player1_prereq_match_id": 214256827,
  "player2_prereq_match_id": 214256828,
  "player1_is_prereq_match_loser": false,
  "player2_is_prereq_match_loser": false,
  "winner_id": 131117246,
  "loser_id": 131142374,
  "started_at": "2020-09-26T21:48:20.605-04:00",
  "created_at": "2020-09-26T19:08:46.782-04:00",
  "updated_at": "2020-09-26T22:49:24.574-04:00",
  "identifier": "W",
  "has_attachment": false,
  "round": 5,
  "player1_votes": null,
  "player2_votes": null,
  "group_id": null,
  "attachment_count": null,
  "scheduled_time": null,
  "location": null,
  "underway_at": null,
  "optional": false,
  "rushb_id": null,
  "completed_at": "2020-09-26T22:49:24.602-04:00",
  "suggested_play_order": 44,
  "forfeited": null,
  "open_graph_image_file_name": null,
  "open_graph_image_content_type": null,
  "open_graph_image_file_size": null,
  "prerequisite_match_ids_csv": "214256827,214256828",
  "scores_csv": "3-1"
}},{
"match": {
  "id": 214256830,
  "tournament_id": 8868290,
  "state": "complete",
  "player1_id": 129701471,
  "player2_id": 129591453,
  "player1_prereq_match_id": 214256822,
  "player2_prereq_match_id": 214256807,
  "player1_is_prereq_match_loser": true,
  "player2_is_prereq_match_loser": true,
  "winner_id": 129701471,
  "loser_id": 129591453,
  "started_at": "2020-09-26T20:14:34.715-04:00",
  "created_at": "2020-09-26T19:08:46.786-04:00",
  "updated_at": "2020-09-26T20:34:24.862-04:00",
  "identifier": "Z",
  "has_attachment": false,
  "round": -1,
  "player1_votes": null,
  "player2_votes": null,
  "group_id": null,
  "attachment_count": null,
  "scheduled_time": null,
  "location": null,
  "underway_at": null,
  "optional": false,
  "rushb_id": null,
  "completed_at": "2020-09-26T20:34:24.876-04:00",
  "suggested_play_order": 24,
  "forfeited": null,
  "open_graph_image_file_name": null,
  "open_graph_image_content_type": null,
  "open_graph_image_file_size": null,
  "prerequisite_match_ids_csv": "214256822,214256807",
  "scores_csv": "2-0"
}},  {
"match": {
  "id": 214256831,
  "tournament_id": 8868290,
  "state": "complete",
  "player1_id": 130912735,
  "player2_id": 131167057,
  "player1_prereq_match_id": 214256821,
  "player2_prereq_match_id": 214256808,
  "player1_is_prereq_match_loser": true,
  "player2_is_prereq_match_loser": true,
  "winner_id": 130912735,
  "loser_id": 131167057,
  "started_at": "2020-09-26T20:24:54.019-04:00",
  "created_at": "2020-09-26T19:08:46.789-04:00",
  "updated_at": "2020-09-26T20:38:49.497-04:00",
  "identifier": "AA",
  "has_attachment": false,
  "round": -1,
  "player1_votes": null,
  "player2_votes": null,
  "group_id": null,
  "attachment_count": null,
  "scheduled_time": null,
  "location": null,
  "underway_at": null,
  "optional": false,
  "rushb_id": null,
  "completed_at": "2020-09-26T20:38:49.601-04:00",
  "suggested_play_order": 23,
  "forfeited": null,
  "open_graph_image_file_name": null,
  "open_graph_image_content_type": null,
  "open_graph_image_file_size": null,
  "prerequisite_match_ids_csv": "214256821,214256808",
  "scores_csv": "2-1"
}

I've tried some other things like this:

function getPage($url, $timeout=25, $header=null){
        $curl = curl_init();
        curl_setopt ($curl, CURLOPT_URL, $url);
        curl_setopt ($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt ($curl, CURLOPT_USERAGENT, sprintf("Mozilla/%d.0",rand(4,5)));
        curl_setopt ($curl, CURLOPT_HEADER, (int)$header);
        curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, 0);
        $html = curl_exec ($curl);
        curl_close ($curl);
        return $html;
        }
        
        $apikey = <removed>;
        $url_matchinfo = "https://api.challonge.com/v1/tournaments/azureod1/matches.json?&api_key=" . $apikey; 
            
            $buffer_matchinfo = getPage($url_matchinfo);
            $buffer_matchinfo_array = json_decode($buffer_matchinfo, true);
            
            usort($buffer_matchinfo_array, function($a, $b){return $a['suggested_play_order'] - $b['suggested_play_order'];});
            echo json_encode($buffer_matchinfo_array); 
    }

And while it changes the order, from what I can tell, it's not chronological. In it's default order, it goes from 1-16 before jumping to 29. Thanks

CorePixels
  • 29
  • 5
  • If you sort by `suggested_play_order`, why do you expect it to be chronological? – Nick Oct 10 '20 at 05:07
  • If i can sort it by suggested_play_order, it'll be from 1-49 (or however many matches there are). The default or has the suggested_play_order, out of order. Maybe numerical order is what i mean, not chronological. – CorePixels Oct 10 '20 at 06:01
  • You need to turn on PHP error reporting, then you would have received the errors about `suggested_play_order` not being a valid array key. You need to use `usort($buffer_matchinfo_array, function($a, $b) { return $a['match']['suggested_play_order'] - $b['match']['suggested_play_order']; });` – Nick Oct 10 '20 at 06:09
  • I turned it on and it returned nothing, however, **YOU DID IT!!** that worked, thank you! I guess it makes sense as they parameters work like parentheses. I may not be proficient but I'm surprised I never figured that out. Really thank you Nick! – CorePixels Oct 10 '20 at 06:17
  • Strange that you're not getting error messages - perhaps they're being logged rather than echo'ed. You should investigate as it's much easier to debug when you can see the errors! :) Anyway, I'm glad that solved your problem. – Nick Oct 10 '20 at 06:21
  • Alright, I will look more into it. – CorePixels Oct 10 '20 at 06:27
  • Also, one more follow-up, how do i save it in that order as a JSON file? "usort($buffer_matchinfo_array, function($a, $b) { return $a['match']['suggested_play_order'] - $b['match']['suggested_play_order']; }); $buffer_matchinfo_arrayfix = json_encode($buffer_matchinfo_array); file_put_contents('matches.json', $buffer_matchinfo_arrayfix);" apparently isn't working as it saves it in the default order – CorePixels Oct 10 '20 at 06:34
  • That should work; `$buffer_matchinfo_arrayfix` should have the sorted JSON in it. See for example: https://3v4l.org/gE09d – Nick Oct 10 '20 at 06:37
  • Ahh it's my bad, I forgot i left a "strtr" on right before the save. :o that messed it up. – CorePixels Oct 10 '20 at 06:41

0 Answers0