将解码后的json解析为php循环

时间:2014-01-30 03:56:14

标签: php json parsing

嗨,有人可以帮助我,我有这个解码的json字符串,我想循环它并获得一些数据..

这是解码的json:

array (size=6)
  0 => 
    object(stdClass)[1]
      public 'date' => 
        object(stdClass)[2]
          public 'day' => int 1
          public 'month' => int 1
          public 'year' => int 2014
          public 'dayOfWeek' => int 3
      public 'localName' => string 'New Year's Day' (length=14)
      public 'englishName' => string 'New Year's Day' (length=14)
  1 => 
    object(stdClass)[3]
      public 'date' => 
        object(stdClass)[4]
          public 'day' => int 27
          public 'month' => int 1
          public 'year' => int 2014
          public 'dayOfWeek' => int 1
      public 'localName' => string 'Australia Day' (length=13)
      public 'englishName' => string 'Australia Day' (length=13)
      public 'note' => string 'Holiday in lieu of 26 Jan 2014' (length=30)
  2 => 
    object(stdClass)[5]
      public 'date' => 
        object(stdClass)[6]
          public 'day' => int 18
          public 'month' => int 4
          public 'year' => int 2014
          public 'dayOfWeek' => int 5
      public 'localName' => string 'Good Friday' (length=11)
      public 'englishName' => string 'Good Friday' (length=11)
  3 => 
    object(stdClass)[7]
      public 'date' => 
        object(stdClass)[8]
          public 'day' => int 21
          public 'month' => int 4
          public 'year' => int 2014
          public 'dayOfWeek' => int 1
      public 'localName' => string 'Easter Monday' (length=13)
      public 'englishName' => string 'Easter Monday' (length=13)
  4 => 
    object(stdClass)[9]
      public 'date' => 
        object(stdClass)[10]
          public 'day' => int 25
          public 'month' => int 12
          public 'year' => int 2014
          public 'dayOfWeek' => int 4
      public 'localName' => string 'Christmas Day' (length=13)
      public 'englishName' => string 'Christmas Day' (length=13)
  5 => 
    object(stdClass)[11]
      public 'date' => 
        object(stdClass)[12]
          public 'day' => int 26
          public 'month' => int 12
          public 'year' => int 2014
          public 'dayOfWeek' => int 5
      public 'localName' => string 'Boxing Day' (length=10)
      public 'englishName' => string 'Boxing Day' (length=10)

我想以某种方式循环播放并获取值日期 englishName

这是我的代码:

foreach ($holidays as $eventobj)
{
    foreach ($eventobj as $event)
    {
        $date = $event[0] . "<BR />";
        $englishName = $event[2] . "<BR />";
    }
}

但这不起作用,有人能告诉我怎么样?谢谢!

1 个答案:

答案 0 :(得分:0)

你正在处理一系列对象,因此......

foreach ($holidays as $event) {
    $englishName = $event->englishName; // kinda redundant

    $date = new DateTime();
    $date->setDate($event->date->year, $event->date->month, $event->date->month);
    // and to set the time to 0
    $date->setTime(0, 0);
}