0

I am trying to figure out how to echo the genre value from an object created with this programme wrapper that requests json data from 'The Movie Database'. I'm so stuck and grappling to understand Object-oriented PHP so any help would be great. I think the fact it is 'nested' (if that's the correct terminology) might be the issue.

<?php 

    include("tmdb/tmdb-api.php");

    $apikey = "myapi_key";
    $tmdb = new TMDB($apikey, 'en', true);
    $idMovie = 206647;

    $movie = $tmdb->getMovie($idMovie);

    // returns a Movie Object
    echo $movie->getTitle().'<br>';
    echo $movie->getVoteAverage().'<br>';
    echo '<img src="'. $tmdb->getImageURL('w185') . $movie->getPoster() .'"/></li><br>';
    echo $movie->genres->id[28]->name;
?>

All of the other values are echoed just fine but I can't seem to get at genres. The json data looks like this ( some of it).

    { 
    "adult":false, 
    "backdrop_path":"\/fa9qPNpmLtk7yC5KZj9kIxlDJvG.jpg",
    "belongs_to_collection":{
        "id":645,
        "name":"James Bond Collection",
        "poster_path":"\/HORpg5CSkmeQlAolx3bKMrKgfi.jpg",
        "backdrop_path":"\/6VcVl48kNKvdXOZfJPdarlUGOsk.jpg" },
        "budget": 0,
        "genres":[
            { "id": 28, "name": "Action" }, 
            { "id": 12, "name": "Adventure" }, 
            { "id": 80, "name": "Crime" } 
        ], 
    "homepage":"http:\/\/www.sonypictures.com\/movies\/spectre\/",
    "id":206647,
    "imdb_id":"tt2379713",
    "original_language":"en",
    "original_title":"SPECTRE",
    "overview":"A cryptic message from Bond\u2019s past sends him on a trail to uncover a sinister organization. While M battles political forces to keep the secret service alive, Bond peels back the layers of deceit to reveal the terrible truth behind SPECTRE."
}
3
  • $movie->genres is an array of genres, so just access them by key like you would a normal array. $movie->genres[0]->name ... You might want to loop over them though, like foreach ($movie->genres as $genre) { echo $genre->name } Commented Oct 3, 2015 at 16:49
  • If you're having trouble reading the json I'd suggest pretty printing it.. echo '<pre>'; print_r($json); Commented Oct 3, 2015 at 16:51
  • It's a json object. Decode your data. Commented Oct 3, 2015 at 16:51

1 Answer 1

2
$movie->genres->id[28]->name

This assumes that id is an array and you want the item with index number 28 from it. What you want is the item containing an id with the value 28 without knowing its index number.

There's no easy way to get to it. You'd have to loop over the array $movie->genres and output the right one. Maybe like this:

$n = 28;
// loop over genres-array
foreach($movie->genres as $i=>$g){
    // check if id of item has right value and if so print it
    if($g->id == $n){
        echo $g->name;
        // skip rest of loop if you only want one 
        break;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. I got this working and returning an expected value. public function getCasts() { return $this->_data['casts']['cast'][0]['name']; } How can I loop through this array to give all the names in this array?
Try something like this: foreach($this->_data['casts']['cast'] as $i=>$c){echo $c['name'];}
php.net/manual/en/control-structures.foreach.php (in case you need clarification about the foreach loop..)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.