0

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'];
        }
  • can you tell us what is the expected output of `print_r($fixture['event_participants'][0]);`? – Sahil Gulati Apr 24 '17 at 07:01
  • The given json encoded data holds an object as value of the `event_participants` property. Why would you expect it to suddenly be an array after copying it? – arkascha Apr 24 '17 at 07:03
  • I think it's because event_participants is a key-value array and the key '0' does not exist. – Prem Raj Apr 24 '17 at 08:27
  • You have an associative array, and are trying to access index 0 which does not exist. You should use `reset` which sets the internal pointer of an array to its first element. Try `print_r(reset($fixture['event_participants']))` to get the value of the first item indexed in the associative array. http://php.net/manual/en/function.reset.php – Gravy Apr 24 '17 at 09:03
  • @arkascha - So ($fixture['event_participants']) contains two(2) 'participant' objects, which i need to access with an index. Im probably not explaining/understanding correctly but i'm expecting to be able to do something like: ['event_participants']['participant'][0] and ['event_participants']['participant'][1] to be able to retrieve those properties. – user1738702 Apr 24 '17 at 11:40
  • @SahilGulati - as above – user1738702 Apr 24 '17 at 11:40
  • @Gravy - this is returning 'Message: reset() expects parameter 1 to be array, null given' – user1738702 Apr 24 '17 at 11:41
  • @user1738702 Can you please update your post with expected result? – Sahil Gulati Apr 24 '17 at 12:00
  • @SahilGulati, post updated with what im trying to achieve – user1738702 Apr 24 '17 at 12:14
  • @user1738702 I am updating a post that will help you out. – Sahil Gulati Apr 24 '17 at 12:16
  • @user1738702 Change your `isset` code to https://eval.in/781766 – Sahil Gulati Apr 24 '17 at 12:18
  • @SahilGulati This isnt giving me the desired output. I need to be able to index through the two event_participants for each fixture – user1738702 Apr 24 '17 at 12:49
  • @user1738702 - `Message: reset() expects parameter 1 to be array, null given` quite simply means that the contents of `$fixture['event_participants']` is `null`. You cannot access as an array if it is null. try `if (isset($fixture['event_participants']) && is_array($fixture['event_participants']))` – Gravy Apr 24 '17 at 13:31

0 Answers0