0
Good night. I’m trying to foreach an api response. She comes to me like this:
{
"page": 1,
"results": [
{
"adult": false,
"backdrop_path": "/9WlJFhOSCPnaaSmsrv0B4zA8iUb.jpg",
"genre_ids": [
28,
27,
53
],
"id": 503736,
"original_language": "en",
"original_title": "Army of the Dead",
"overview": "Após um surto de zumbis em Las Vegas, nos Estados Unidos, um grupo de mercenários faz uma aposta final, aventurando-se na zona de quarentena para tentar realizar o maior assalto de todos os tempos.",
"poster_path": "/vARBnfGEcsb83gdtK0JHb8QaLio.jpg",
"release_date": "2021-05-14",
"title": "Army of the Dead: Invasão em Las Vegas",
"video": false,
"vote_average": 6.6,
"vote_count": 128,
"popularity": 313.796,
"media_type": "movie"
},
{
"adult": false,
"backdrop_path": "/6ELCZlTA5lGUops70hKdB83WJxH.jpg",
"genre_ids": [
28,
14,
12
],
"vote_count": 2563,
"original_language": "en",
"original_title": "Mortal Kombat",
"poster_path": "/w8BVC3qrCWCiTHRz4Rft12dtQF0.jpg",
"id": 460465,
"video": false,
"title": "Mortal Kombat",
"vote_average": 7.6,
"overview": "Nova aventura baseada no videogame Mortal Kombat. Na história, um jovem que nunca treinou artes marciais acaba envolvido em um gigantesco torneio de luta envolvendo guerreiros da Terra e lutadores e outras dimensões.",
"release_date": "2021-04-07",
"popularity": 2112.759,
"media_type": "movie"
},
{
"adult": false,
"backdrop_path": "/gUttUEqsrvaMlK5oL5TSQ54iE96.jpg",
"genre_ids": [
80,
18,
9648,
53
],
"id": 520663,
"original_language": "en",
"original_title": "The Woman in the Window",
"overview": "A Dr.ª Anna Fox é uma alcoólatra reclusa que passa os dias em seu apartamento em Nova York assistindo a filmes antigos e observando seus vizinhos. Quando a família Russell se muda para o prédio da frente, ela passa a espionar o que seria a família perfeita, até testemunhar uma cena chocante que muda sua vida.",
"poster_path": "/8BasMP1hUZtDx0khdbHBkVZhKjJ.jpg",
"release_date": "2021-05-14",
"title": "A Mulher na Janela",
"video": false,
"vote_average": 6.3,
"vote_count": 545,
"popularity": 366.666,
"media_type": "movie"
},.....
$json = file_get_contents($url);
I’ve tried with json_recode, with and without true. But when I try to read with foreach it returns me only written
Array
I tried with the following code
foreach($json as $result) { echo $result[0].'
'; }
I have tried several ways, I have tried to replace the beginning {pages:1, Results: but it returns that the classStd does not allow. I was able to get the API data but I can’t handle it.
Utilize
$result = json_decode($json, true);
to transform the JSON in a Array. If you try to use theecho
in a Array, the printed value will be Array. It is necessary to pass an index asforeach($json['results'] as $result) { echo $result['backdrop_path']; }
– Valdeir Psr
Show. It worked out my friend. agr is to treat each individual key. Thanks. If you want to send as answer so I can evaluate better.
– Clayton Prebelli