Bram Huisman
Bram Huisman

Reputation: 135

Loop over Array in array

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,

Upvotes: 0

Views: 51

Answers (2)

Rahul
Rahul

Reputation: 18577

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
}

Upvotes: 1

Paresh Gami
Paresh Gami

Reputation: 4792

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>";
}
?>

Upvotes: 1

Related Questions