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