Reputation: 35
I'm Having issues grabbing values in a multi level array.
This is my JSON I am grabbing with CURL and putting into a variable.
{
"id": 454626,
"results": [
{
"iso_3166_1": "SK",
"release_dates": [
{
"certification": "U",
"iso_639_1": "sk",
"note": "",
"release_date": "2020-02-20T00:00:00.000Z",
"type": 3
}
]
},
{
"iso_3166_1": "DE",
"release_dates": [
{
"certification": "6",
"iso_639_1": "",
"note": "",
"release_date": "2020-02-13T00:00:00.000Z",
"type": 3
}
]
},
{
"iso_3166_1": "TW",
"release_dates": [
{
"certification": "G",
"iso_639_1": "",
"note": "",
"release_date": "2020-02-21T00:00:00.000Z",
"type": 3
}
]
}
]
}
This is my PHP code that I'm having issues with.
$id_tmdb = $row[id_tmdb];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.themoviedb.org/3/movie/$id_tmdb/release_dates?api_key=API-KEY",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET"
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$result = json_decode($response, true);
foreach($result[results][release_dates] as $key=>$val){
echo "$val[release_date]";
}
I'm trying to get the release_date
but it just shows blank.
I have another page that is formatted the same way so I am unsure why it isn't working.... Thank you for all your help.
Upvotes: 0
Views: 53
Reputation: 147166
Your $result['results']
value is also an array and you need to iterate over that as well as the release_dates
array:
foreach($result['results'] as $results) {
foreach ($results['release_dates'] as $val){
echo "$val[release_date]\n";
}
}
Output (for the sample JSON provided):
2020-02-20T00:00:00.000Z
2020-02-13T00:00:00.000Z
2020-02-21T00:00:00.000Z
Upvotes: 2