Search code examples
phparraysapielement

PHP get first array element


Hey all i am trying to get the data from a movie API. The format is this:

page => 1
results =>
   0 =>
    adult =>
    backdrop_path => /gM3KKixSicG.jpg
    id => 603
    original_title => The Matrix
    release_date => 1999-03-30
    poster_path => /gynBNzwyaioNkjKgN.jpg
    popularity => 10.55
    title => The Matrix
    vote_average => 9
    vote_count => 328
  1 =>
    adult =>
    backdrop_path => /o6XxGMvqKx0.jpg
    id => 605
    original_title => The Matrix Revolutions
    release_date => 2003-10-26
    poster_path => /sKogjhfs5q3aEG8.jpg
    popularity => 5.11
    title => The Matrix Revolutions
    vote_average => 7.5
    vote_count => 98
 etc etc....

How can i get only the first element [0]'s data (as in backdrop_path, original_title, etc etc)? I'm new at PHP arrays :).

And of course this is what i used to output my array data:

 print_r($theMovie)

Any help would be great!


Solution

  • You can point to the array with this $theMovie['result'][0]['backdrop_path']; or you can loop through it like this,

    foreach($theMovie['results'] as $movie){
       echo $movie['backdrop_path'];
    }