0

I've got this data: http://api.tvmaze.com/shows?page=0 What i am trying to achieve is looping over the data and echo each id. This is the code i used:

<?php

$url = ('http://api.tvmaze.com/shows?page=0');
$json = file_get_contents($url);

$response = json_decode($json, TRUE);

foreach( $response as $serie){

  foreach( $serie as $info => $value){
    echo $info->$value["id"];
  }

}

I don't really know what i am doing wrong.. Do you guys have any idea?

Greetz,

2
  • What is your array in $response? Commented May 24, 2017 at 10:48
  • print_r($value) and check if you are getting data ? Commented May 24, 2017 at 10:49

2 Answers 2

1

Try below code for getting id and url. You have to use array because you are passing TRUE in json_decode().

<?php

$url = ('http://api.tvmaze.com/shows?page=0');
$json = file_get_contents($url);

$response = json_decode($json, TRUE);

foreach( $response as $serie)
{
    echo $serie['id']."->".$serie['url']."<br>";
}
?>
Sign up to request clarification or add additional context in comments.

Comments

1

Keep it simple to fetch ids you wanted,

$url = ('http://api.tvmaze.com/shows?page=0');
$json = file_get_contents($url);
$response = json_decode($json, TRUE);
echo "<pre>";

foreach ($response as  $value) {
    echo $value['id']."<br/>"; // you will get ids in here only
}

Comments

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.