0

i want display the backdrop_path of the movie but i get this error

Notice: Undefined index: backdrop_path in /..../public_html/movie.php on line 167

I have added

print_r($tmdbResult);

to see the content of $tmdbResult. I want to display just 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
    (
    )

)

The code i use is

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);
P.focus
  • 41
  • 4
  • 1
    `$tmdbResult['movie_results'][0]['backdrop_path'];` `[0], Carl` – u_mulder Sep 24 '15 at 19:26
  • i replace that with $backdrop_path = $tmdbResult['movie_results']['backdrop_path']; ? – P.focus Sep 24 '15 at 19:26
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – andrewsi Sep 25 '15 at 01:27

1 Answers1

1

The reason is you're getting a numeric index, because the result can contain multiple movies. In this case you're getting just one.

All you need to do is access the array like this @u_mulder said:

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

The [0] meaning that in case you get multiple results, just need to change that index to access the others. May be you shoulds think of a foreach loop, unless for some reason of yours, you KNOW you'll always get ONE single movie, in that case, hardcode your $bp = $tmdbResult['movie_results'][0]['backdrop_path']; no problem.

Juan Bonnett
  • 1,823
  • 1
  • 15
  • 33