evan
evan

Reputation: 964

PHP foreach loop with JSON Data

I am using the BandsInTown API, and I'm trying to list all the event titles for a band. The code below works for the first title, but I need to look through all of them. I tried a foreach loop I found on here, but did have any luck. How do I show all the event titles for a particular band? I have been looking at previous questions like this one.

$json = file_get_contents("http://api.bandsintown.com/artists/Skrillex/events.json?api_version=2.0&app_id=MYID");
$data = json_decode($json);
echo $data[0]->title;

// I tried this, but it didn't work
foreach($data->title as $title){
   echo $title;
}        

Upvotes: 0

Views: 778

Answers (3)

Krish R
Krish R

Reputation: 22711

Try this.,

foreach($data as $Events){
    echo $Events->title."<br>"; 
}

Upvotes: 2

Brian H.
Brian H.

Reputation: 505

Try..

foreach($data as $item){
   echo $item->title; 
}

Upvotes: 0

Scopey
Scopey

Reputation: 6319

I think you might be look for something like

foreach($data as $datum){
    echo $datum->title;
}

Upvotes: 3

Related Questions