PHP错误'未定义的偏移量:0'在json_decode之后

时间:2017-04-24 06:59:07

标签: php arrays json

我是PHP的新手,我有一个json文件,其中包含几个'事件'看起来如下的对象。这是一个事件':

"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"
            }
        }
    }
}

我已经使用php解码成了一个php数组,但是event_participants对象没有转换成数组,或者如果它们是,当我尝试访问$fixture['event_participants'][0]时我得到{{1错误。我的代码如下:

Message: Undefined offset: 0

如果我$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]); } } 我希望收到:

$fixture['event_participants'][0]

所以我回答了自己的问题 - array_values解决了我的问题:

       {
            "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"
            }
        }

0 个答案:

没有答案