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!

StealthRT
  • 10,108
  • 40
  • 183
  • 342

6 Answers6

7

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.

Valery Viktorovsky
  • 6,487
  • 3
  • 39
  • 47
2

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'];
}
believe me
  • 910
  • 1
  • 5
  • 24
2

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.

Daren Chandisingh
  • 2,157
  • 1
  • 13
  • 17
1

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

$results = $datas['results'];
$theMovie = $results[0];
zessx
  • 68,042
  • 28
  • 135
  • 158
0

Try

$yourArray['results'][0]

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

nekaab
  • 442
  • 3
  • 15
0

To start learning a bit I would suggest that you use: array_slice

Now your code in PHP would look like:

$myVariable = [
    "page" => 1,
    "results" => [
        [
            'adult' => 1,
            '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,
        ],
        [
            'adult' => 2,
            'backdrop_path' => "/gM3KKixSicG.jpg",
            'id' => 603,
            'original_title' => "The Matrix Revolutions",
            'release_date' => "1999-03-30",
            'poster_path' => "/gynBNzwyaioNkjKgN.jpg",
            'popularity' => 10.55,
            'title' => "The Matrix Revolutions",
            'vote_average' => 9,
            'vote_count' => 328,
        ],
    ],
];

Then on the above one you use:

$newArray = array_slice($myVariable['results'], 0, 1);

The output of the command above would be:

[
  [
    "adult" => 1,
    "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,
  ],
]

Now if you want to get a flat array, because above one is still multidimensional, you simple use:

$newArray[0]

The output would be:

[
  "adult" => 1,
  "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,
]

OR if you want one liner with array_slice add index 0 at the end of the command:

$newArray = array_slice($myVariable['results'], 0, 1)[0];

Hope this helps :). Cheers.

Matija
  • 17,604
  • 2
  • 48
  • 43