简体   繁体   中英

PHP foreach loop on JSON data from Bandsintown

Writing my own bandsintown feed as I don't like the default widgets etc they supply - RSVP popup is ugly

Successfully outputted an array title for the first event, what I'm missing is how to run a foreach through the results as they don't have a containing array to target

<?php
    //Get the list of events from the function
    $json = file_get_contents("http://api.bandsintown.com/artists/kiss/events.json?api_version=2.0&app_id=kiss");

    $data = json_decode($json, true);

    //var_dump($data);

     echo $data->title;

?>

The outputted JSON doesn't have an array name to target, so I can't say foreach($data as $arrayname)

  array (size=22)
    0 => 
      array (size=13)
        'id' => int 9722885
        'title' => string 'Kiss @ Rock im Revier 2015 in Gelsenkirchen, Germany' (length=52)
        'datetime' => string '2015-05-29T00:00:00' (length=19)
        'formatted_datetime' => string 'Friday, May 29, 2015 at 12:00AM' (length=31)
        'formatted_location' => string 'Gelsenkirchen, Germany' (length=22)
        'ticket_url' => string 'http://www.bandsintown.com/event/9722885/buy_tickets?app_id=kiss&artist=Kiss' (length=76)
        'ticket_type' => string 'Tickets' (length=7)
        'ticket_status' => string 'available' (length=9)
        'on_sale_datetime' => null
        'facebook_rsvp_url' => string 'http://www.bandsintown.com/event/9722885?app_id=kiss&artist=Kiss&came_from=67' (length=77)
        'description' => string 'Der Ring 2015' (length=13)
        'artists' => 
          array (size=1)
            0 => 
              array (size=8)
                ...
        'venue' => 
          array (size=6)
            'name' => string 'Rock im Revier 2015' (length=19)
            'city' => string 'Gelsenkirchen' (length=13)
            'region' => string '07' (length=2)
            'country' => string 'Germany' (length=7)
            'latitude' => float 51.5166667
            'longitude' => float 7.05
    1 => 
      array (size=13)
        'id' => int 8995518
        'title' => string 'Kiss @ O2 World Hamburg in Hamburg, Germany' (length=43)

Specifying true to json_decode makes it return associative arrays, not objects, so you should use ['title'] rather than ->title .

$data = json_decode($json, true);
foreach ($data as $song) {
    echo $song['title'] . "<br>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM