[Solved] Access data in an array


Its hard to tell because you didnt post the code you are using but i suspect you are confusing the structure type. What you posted is actually an object – an instance of stdClass and the items that look like array elements are in fact properties on that object. So you need to use the appropriate access method of ->propertyName as opposed to ['propertyName']:

// YES
echo $data->fixtures[0]->homeTeamName;

// NO
echo $data['fixtures'][0]['homeTeamName'];

So to get the data for each game (or what i presume to be the “game” items) you would do:

foreach ($data->fixtures as $game) {
    echo $game->homeTeamName;
    echo $game->awayTeamName;
    echo $game->result->goalsHomeTeam;
    echo $game->result->goalsAwayTeam;
    // etc..
}

It also seems like this is the return value from an API parsed with json_decode(). If that is the case you can actually make it an array all the way through by passing true as the second argument to json_decode():

$data = json_decode($response, true);

foreach ($data['fixtures'] as $game) {
    echo $game['homeTeamName'];
    echo $game['awayTeamName'];
    echo $game['result']['goalsHomeTeam'];
    echo $game['result']['goalsAwayTeam'];
    // etc..
} 

3

solved Access data in an array