I WANT TO ACCESS TO time, home_scorer, score, away_scorer AND I CA N'T GIVE IT :/ THANK YOU FOR YOUR HELP!
[
{
"match_id":"119596",
"country_id":"169",
"country_name":"England",
"league_id":"62",
"league_name":"Premier League",
"match_date":"2016-10-30",
"match_status":"FT",
"match_time":"18:00",
"match_hometeam_name":"Southampton",
"match_hometeam_score":"0",
"match_awayteam_name":"Chelsea",
"match_awayteam_score":"2",
"match_live":"1",
"goalscorer": [
{
"time":"6'",
"home_scorer":"",
"score":"0 - 1",
"away_scorer":"E. Eden Hazard"
},
{
"time":"55'",
"home_scorer":"",
"score":"0 - 2",
"away_scorer":"D. Diego Costa"
}
]
}
]
Imagine that you have the array stored in a variable called
$array
, so you should access the first position of the array where it is locatedgoalscorer
as follows:This way we get the array
goalscorer
.Later we can go through each object of the array with a loop
foreach
:Always remember that arrays are defined in JSON using square brackets
[]
and each of the objects defined in an array using curly braces{}
.Since it is an array in JSON format, you must first decode it to an array that PHP can use as follows:
After that you can access its elements and attributes as you occupy it:
The reason why I use it
->
is because when doing the functionjson_decode()
the array is converted into an object, so in order to access its attributes you must use that notation instead of the traditional one with boxes[]
.Note: The reason I used
\
in the attributetime
was because otherwise PHP would think that I am closing the JSON string there and would send an error when compiling.