Im new to PHP, I have a json file that contains several 'events' objects that look like the following. This is one 'event':
"2252182": {
"id" : "2252182",
"name" : "Arsenal-Everton",
"tournament_stageFK": "844714",
"startdate" : "2017-05-21T14:00:00+00:00",
"status_type" : "notstarted",
"property" : {},
"event_participants": {
"7297686": {
"id" : "7297686",
"number" : "1",
"participantFK": "9825",
"eventFK" : "2252182",
"n" : "0",
"ut" : "2016-06-15T08:05:33+00:00",
"result" : {},
"participant" : {
"id" : "9825",
"name" : "Arsenal",
"gender" : "male",
"type" : "team",
"countryFK" : "2",
"n" : "2",
"ut" : "2016-09-15T10:44:36+00:00",
"country_name": "England"
}
},
"7297687": {
"id" : "7297687",
"number" : "2",
"participantFK": "8668",
"eventFK" : "2252182",
"n" : "0",
"ut" : "2016-06-15T08:05:33+00:00",
"result" : {
"24255418": {
"id" : "24255418",
"event_participantsFK": "7297687",
"result_typeFK" : "1",
"result_code" : "ordinarytime",
"value" : "0",
"n" : "0",
"ut" : "2016-06-15T08:05:33+00:00"
},
"24255420": {
"id" : "24255420",
"event_participantsFK": "7297687",
"result_typeFK" : "6",
"result_code" : "runningscore",
"value" : "0",
"n" : "0",
"ut" : "2016-06-15T08:05:33+00:00"
}
},
"participant" : {
"id" : "8668",
"name" : "Everton",
"gender" : "male",
"type" : "team",
"countryFK" : "2",
"n" : "2",
"ut" : "2016-09-15T10:45:47+00:00",
"country_name": "England"
}
}
}
}
I have used php to decode into a php array, but the event_participants
objects are not being converted into arrays, or if they are, when I try and access $fixture['event_participants'][0]
I am getting Message: Undefined offset: 0
error. My code is below:
$json = json_decode(file_get_contents('assets/json/fixtures.json'),true);
foreach ($json['events'] as $fixture)
{
$tmp = array();
$tmp['group_id'] = $fixture['tournament_stageFK'];
$tmp['group_league'] = $fixture['tournament_stage_name'];
$tmp['fixture_id'] = $fixture['id'];
$tmp['fixture_name'] = $fixture['name'];
$tmp['fixture_date'] = $fixture['startdate'];
$tmp['fixture_status'] = $fixture['status_type'];
$tmp['event_participants'] = $fixture['event_participants'];
if (isset($fixture['event_participants'])) {
print_r($fixture['event_participants'][0]);
}
}
If I do $fixture['event_participants'][0]
I expect to receive:
{
"id" : "7297686",
"number" : "1",
"participantFK": "9825",
"eventFK" : "2252182",
"n" : "0",
"ut" : "2016-06-15T08:05:33+00:00",
"result" : {},
"participant" : {
"id" : "9825",
"name" : "Arsenal",
"gender" : "male",
"type" : "team",
"countryFK" : "2",
"n" : "2",
"ut" : "2016-09-15T10:44:36+00:00",
"country_name": "England"
}
}
So I answered my own query - array_values solved my issue:
if (isset($fixture['event_participants'])) {
$num = array_values($fixture['event_participants']);
$tmp['participant_1_id'] = $num[0]['participant']['id'];
$tmp['participant_1_name'] = $num[0]['participant']['name'];
$tmp['participant_2_id'] = $num[1]['participant']['id'];
$tmp['participant_2_name'] = $num[1]['participant']['name'];
}