3

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!

3
  • 3
    Is ...->results[0] ok? Commented Mar 15, 2013 at 16:07
  • you seem to be answering yourself on your question... $theMovie["results"][0] ? Commented Mar 15, 2013 at 16:08
  • see stackoverflow.com/questions/1921421/… Commented Jul 18, 2014 at 18:19

5 Answers 5

6

Another solution:

$arr = reset($datas['results']);

Returns the value of the first array element, or FALSE if the array is empty.

OR

$arr = current($datas['results']);

The current() function simply returns the value of the array element that's currently being pointed to by the internal pointer. It does not move the pointer in any way. If the internal pointer points beyond the end of the elements list or the array is empty, current() returns FALSE.

Sign up to request clarification or add additional context in comments.

Comments

1

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'];
}

2 Comments

I'm not getting any data back from the code above BUT i am getting data back with the $theMovie['result'][0]['backdrop_path'];??
Yes, I'm missing something check it again.
1

You can use array_shift to pop off the first element, then check that it is valid (array_shift will return null if there are no results or the item isn't an array).

$data = array_shift($theMovie['results']);
if (null !== $data) {
    // process the first result
}

If you want to iterate though all the results, you can do a foreach loop while loop with array_shift.

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

while ($data = array_shift($theMovie['results'])) {
    echo $data['backdrop_path'];
}

Or just use $theMovie['result'][0]['backdrop_path']; as already suggested, after checking that $theMovie['result'][0] is actually set.

1 Comment

Gave you +1 for helping me out, Daren C.!
0

Assuming all this code is stored in a variable $datas :

$results = $datas['results'];
$theMovie = $results[0];

Comments

0

Try

$yourArray['results'][0]

But keep in mind this produces errors when the result array is empty.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.