I have this json file: and I would like to get all the title so my code is: var_dump($json['results'][0]['title']); but it getting just one title, I know I need to do a foreach but I don't know how :( so if you could help me that will be great ! Thanks
Asked
Active
Viewed 68 times
-1

ZaraaKai
- 85
- 1
- 1
- 5
-
2`foreach ($json['results'] as $result) echo $result['title'];` – Nick Aug 31 '18 at 01:41
-
1Possible duplicate of [How can I parse a JSON file with PHP?](https://stackoverflow.com/questions/4343596/how-can-i-parse-a-json-file-with-php) – Nick Aug 31 '18 at 01:44
3 Answers
1
Your data is coming back as an array with item's inside of it. This mean's you'll need to loop through this JSON and print each item;
foreach($json['results'] as $movie) {
echo $movie['title'] . "<br />";
}

Darren
- 13,050
- 4
- 41
- 79
-
Hi, I have a another problems I want for every film in the json file a new thing to appears in my page so I did this code and this appear :( @Darren I think I need to to a while ? but i dont know how https://imgur.com/a/kffIebu – ZaraaKai Aug 31 '18 at 02:39
-
You need to modify your HTML that you put in there to replace the image and description as you require. – Darren Aug 31 '18 at 03:31
-
1
You will want to loop through all of the JSON results objects and echo or store the 'title' value;
foreach ($json['results'] as $object) {
//Option 1
echo $object['title'];
//Option 2
array_push($titles, $object['title'];
}

Dawson Seibold
- 145
- 2
- 11
0
Assuming that the variable $json contains your data, I see two equivalent options in order to iterate trough the array
Using for
for($index = 0; $index < count($json['results']); $index++) { echo $json['results'][$index]['title'] . "\n"; }
Using foreach
foreach($json['results'] as $movie) { echo $movie['title'] . "\n"; }

Adriel Santos
- 653
- 7
- 15