-1

I have this json file: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

ZaraaKai
  • 85
  • 1
  • 1
  • 5
  • 2
    `foreach ($json['results'] as $result) echo $result['title'];` – Nick Aug 31 '18 at 01:41
  • 1
    Possible 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 Answers3

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
  • modify what pls – ZaraaKai Aug 31 '18 at 14:20
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

  1. Using for

    for($index = 0; $index < count($json['results']); $index++) {
        echo $json['results'][$index]['title'] . "\n";
    }
    
  2. Using foreach

    foreach($json['results'] as $movie) {
        echo $movie['title'] . "\n";
    }
    
Adriel Santos
  • 653
  • 7
  • 15