print_r数组到变量

时间:2014-06-08 19:09:21

标签: php arrays variables

我是一个PHP新手,所以这可能很容易,但我不能弄明白。我正在使用一个脚本,使用API​​从电影数据库'TMDB'中提取信息。

以下代码:

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

打印出此信息:

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
            )

等等。

我想要做的是提取信息并将其保存为自己的变量。例如:

[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

这将是第一部电影,所以我可以将其[id]保存为$id,将[original_title]保存为$title等等,这样我就可以在页面的任何位置调用这些变量

这可能吗?

我基本上想从数据库中获取所有信息并将其显示在PHP文件中但不是通过打印数组

任何信息都将非常感激。

2 个答案:

答案 0 :(得分:0)

像这样访问你的元素:

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

   $your_variable=$array["id"]; // and so on...
}

答案 1 :(得分:0)

您可以使用当前的阵列单独定位影片

$playing['results'][0]['title']  // would give you the first movie title
$playing['results'][1]['title']  // would give you the second movie title