I can’t get an array

Asked

Viewed 27 times

0

I’m trying to get a value inside an array, but it doesn’t print out anything from the id field, which is the field I want to pick up

$jsonc = file_get_contents("https://api.themoviedb.org/3/search/tv?query=todo%20o%20mundo%20odeia%20o%20chris&api_key=b5ad6a9f75ea4e476b5f08b524ddf83d");
$epjson = json_decode($jsonc);    
print_r($epjson -> id);

NOTE: When I do only the print_r($epjson); print that:

stdClass Object ( [page] => 1 [total_results] => 1 [total_pages] => 1 [Results] => Array ( [0] => stdClass Object ( [original_name] => Everybody Hates Chris [id] => 252 [name] => Everybody Hates Chris [vote_count] => 68 [vote_average] => 6.82 [poster_path] => /dM0IUKmrjyhFskt0ZBMbbfWxRIQ.jpg [first_air_date] => 2005-09-22 [Popularity] => 10.150596 [genre_ids] => Array ( [0] => 35 ) [original_language] => en [backdrop_path] => /xYLcbJoQEowXFVqe94A37CIC0Tq.jpg [Overview] => Everybody Hates Chris is an American television narrative sitcom that depicts the Troubled Teenage Experiences of Comedian Chris Rock while Growing up in Bedford-Stuyvesant, Brooklyn, New York City. The show is set between 1982 and 1987, but Rock himself was a teenager between 1978 and 1983. Rock Grew up with a boy named Kenny Montero, Whom he has often referred to as the Inspiration for a Lot of the episodes. In Many of his interviews, Rock has described Kenny as the Reason he got into comedy in the first place. The show’s title parodies the hit CBS sitcom Everybody Loves Raymond, in which Rock stated: "Everybody Loves Raymond, but Everybody Hates Chris!". The show’s lead Actors are Tyler James Williams, Terry Crews, Tichina Arnold, Tequan Richmond, Imani Hakim, and Vincent Martella. In 2008, the CW Moved Everybody Hates Chris and The Game to the Friday night death slot. The Fourth Season of the series premiered Friday, October 3, 2008, at 8:00PM Eastern/7:00PM Central. On May 21, 2009, The CW announced that it had cancelled Everybody Hates Chris. Prior to this, Rock announced that the end of Season 4 Matched up with his Own Past-dropping out of high school to become a Comedian-and that it was time to end the show. [origin_country] => Array ( [0] => US ) ) ) )

1 answer

1


Look at the structure of the information in the API you are using:

{
    "page": 1,
    "total_results": 1,
    "total_pages": 1,
    "results": [{
                "original_name": "Everybody Hates Chris",
                "id": 252,
                "name": "Everybody Hates Chris",
                "vote_count": 68,
                ....

The id is not at the root but inside the array results, so you should access the same with:

print_r($epjson->results[0]->id);

If you have more than one result and want the id of the various results should use a for to go through them.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.