注意:未定义的索引backdrop_path

时间:2015-09-24 19:24:51

标签: php json api

我希望显示电影的backdrop_path但我收到此错误

  

注意:未定义的索引:第167行的/..../public_html/movie.php中的backdrop_path

我添加了

print_r($tmdbResult);

查看$tmdbResult的内容。我想只显示backdrop_path

Array
(
[movie_results] => Array
    (
        [0] => Array
            (
[adult] => 
[backdrop_path] => /mbA7SCtJoFTactP1lDHA055qCf.jpg
                [genre_ids] => Array
                    (
                        [0] => 35
                        [1] => 28
                    )

                [id] => 261392
                [original_language] => en
                [original_title] => American Ultra
                [overview] => A stoner and his girlfriend's sleepy, small-town existence is disrupted when his past comes back to haunt him in the form of a government operation set to wipe him out.
                [release_date] => 2015-08-21
                [poster_path] => /6oGHH27nqaLGfpcgYRIZYSJs7AD.jpg
                [popularity] => 3.509263
                [title] => American Ultra
                [video] => 
                [vote_average] => 5.6
                [vote_count] => 134
            )

    )

[person_results] => Array
    (
    )

[tv_results] => Array
    (
    )

[tv_episode_results] => Array
    (
    )

[tv_season_results] => Array
    (
    )

)

我使用的代码是

ini_set("display_errors",1);


$tmdbResponse = curl_exec($ch);
curl_close($ch);
$tmdbResult = json_decode($tmdbResponse, true );
$backdrop_path = $tmdbResult['movie_results']['backdrop_path'];
$smarty->assign("backdrop_path",$backdrop_path);
print_r($tmdbResult);

1 个答案:

答案 0 :(得分:1)

原因是您正在获取数字索引,因为结果可能包含多部电影。在这种情况下,您只能获得一个。

您需要做的就是像@u_mulder所说的那样访问数组:

$bp = $tmdbResult['movie_results'][0]['backdrop_path'];

[0]意味着如果您获得多个结果,只需更改该索引即可访问其他索引。也许你应该想到一个foreach循环,除非出于某种原因,你知道你总是得到一部电影,在这种情况下,硬编码你的$bp = $tmdbResult['movie_results'][0]['backdrop_path'];没问题。