PHP foreach循环来自Bandsintown的JSON数据

时间:2015-05-18 23:55:45

标签: php arrays json foreach

编写我自己的bandintown提要,因为我不喜欢他们提供的默认小部件等 - RSVP popup很难看

成功输出第一个事件的数组标题,我缺少的是如何在结果中运行foreach,因为它们没有包含目标的数组

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

?>

输出的JSON没有目标数组名称,所以我不能说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)

1 个答案:

答案 0 :(得分:2)

true指定为json_decode会使其返回关联数组,而非对象,因此您应使用['title']而不是->title

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