-1

I am a bit of a PHP newbie so this is probably easy but I just cant figure it out. I'm using a script that pulls info from the movie database 'TMDB' using there API.

The following code:

$playing = $tmdb_V3->nowPlayingMovies($page=1);
echo"<pre>";print_r($playing);echo"</pre>";

prints out this info:

Array
(
[dates] => Array
    (
        [minimum] => 2014-05-20
        [maximum] => 2014-06-10
    )

[page] => 1
[results] => Array
    (
        [0] => Array
            (
                [adult] => 
                [backdrop_path] => /aBkkrhQS4rO3u1OTahywtSXu3It.jpg
                [id] => 127585
                [original_title] => X-Men: Days of Future Past
                [release_date] => 2014-05-23
                [poster_path] => /qKkFk9HELmABpcPoc1HHZGIxQ5a.jpg
                [popularity] => 232.77188788256
                [title] => X-Men: Days of Future Past
                [vote_average] => 7.2
                [vote_count] => 178
            )

        [1] => Array
            (
                [adult] => 
                [backdrop_path] => /9bd6IFBMwSOINrEW8VIBxaS4cd9.jpg
                [id] => 102651
                [original_title] => Maleficent
                [release_date] => 2014-05-30
                [poster_path] => /1cFVCUYKSBuEUDoVftKvqcfuIgc.jpg
                [popularity] => 57.46008792307
                [title] => Maleficent
                [vote_average] => 7.3
                [vote_count] => 42
            )

        [2] => Array
            (
                [adult] => 
                [backdrop_path] => /7mgKeg18Qml5nJQa56RBZO7dIu0.jpg
                [id] => 137113
                [original_title] => Edge of Tomorrow
                [release_date] => 2014-06-06
                [poster_path] => /zMP1PEvwH8Uy6jiIG1Qzp0svS1q.jpg
                [popularity] => 56.13626675
                [title] => Edge of Tomorrow
                [vote_average] => 7.5
                [vote_count] => 29
            )

and so on.

What I would like to do is extract the information and save it as its own variable. so for example:

[0] => Array
        (
            [adult] => 
            [backdrop_path] => /aBkkrhQS4rO3u1OTahywtSXu3It.jpg
            [id] => 127585
            [original_title] => X-Men: Days of Future Past
            [release_date] => 2014-05-23
            [poster_path] => /qKkFk9HELmABpcPoc1HHZGIxQ5a.jpg
            [popularity] => 232.77188788256
            [title] => X-Men: Days of Future Past
            [vote_average] => 7.2
            [vote_count] => 178

Would be the first movie so I can then save its [id] as $id, the [original_title] as $title and so on, so I can call on those variables any where on the page.

Is this possible?

I basically want to get all the info from the database and display it in a PHP file but not by printing the array

any info will be very much appreciated.

AdrianHHH
  • 13,492
  • 16
  • 50
  • 87
  • You can, but the whole idea of arrays is going down. You know, you can access certain array keys? – Royal Bg Jun 08 '14 at 19:12
  • @UrbanTanCreams: It's in the PHP manual pretty well described with many examples. You find it also explained in the linked duplicated question. The accepted answer there does also contain links to the PHP manual for the sections in question. – hakre Jun 08 '14 at 19:22
  • And if you want to pass such an array entry as variables into a HTML fragment, the following include helper might be useful: [Is include()/require() with “side effects” a bad practice?](http://stackoverflow.com/a/7697490/367456) – hakre Jun 08 '14 at 19:27

2 Answers2

0

Access your elements like this:

foreach($playing["results"] as $array){

   $your_variable=$array["id"]; // and so on...
}
iCode
  • 1,456
  • 1
  • 15
  • 26
0

You can target the movies individually with the array as it currently is

$playing['results'][0]['title']  // would give you the first movie title
$playing['results'][1]['title']  // would give you the second movie title
Kevin Lynch
  • 24,427
  • 3
  • 36
  • 37