【问题标题】:foreach: how to iterate thisforeach:如何迭代这个
【发布时间】:2010-03-30 08:32:17
【问题描述】:
object(stdClass)[1] 
  public 'inbox' =>  
    array 
      0 =>  
        object(stdClass)[2] 
          public 'from' => string '55512351' (length=8) 
          public 'date' => string '29/03/2010' (length=10) 
          public 'time' => string '21:24:10' (length=8) 
          public 'utcOffsetSeconds' => int 3600 
          public 'recipients' =>  
            array 
              0 =>  
                object(stdClass)[3] 
                  public 'address' => string '55512351' (length=8) 
                  public 'name' => string '55512351' (length=8) 
                  public 'deliveryStatus' => string 'notRequested' (length=12) 
          public 'body' => string 'This is message text.' (length=21) 
      1 =>  
        object(stdClass)[4] 
          public 'from' => string '55512351' (length=8) 
          public 'date' => string '29/03/2010' (length=10) 
          public 'time' => string '21:24:12' (length=8) 
          public 'utcOffsetSeconds' => int 3600 
          public 'recipients' =>  
            array 
              0 =>  
                object(stdClass)[5] 
                  public 'address' => string '55512351' (length=8) 
                  public 'name' => string '55512351' (length=8) 
                  public 'deliveryStatus' => string 'notRequested' (length=12) 
          public 'body' => string 'This is message text.' (length=21) 
      .... 
      .... 

我有这个 foreach,但它不会迭代 addressnamedeliveryStatus!您能说明如何获取这些数据吗?

foreach ($data->inbox as $note) {
  echo '<p>';
  echo 'From : ' . htmlspecialchars($note->from) . '<br />';
  echo 'Date : ' . htmlspecialchars($note->date) . '<br />';
  echo 'Time : ' . htmlspecialchars($note->time) . '<br />';
  echo 'Body : ' . htmlspecialchars($note->body) . '<br />';
}

【问题讨论】:

    标签: php arrays foreach loops


    【解决方案1】:

    地址、名称、deliveryStatus 位于子数组中,因此您需要遍历子数组才能提取要打印的数据

    foreach ($data->inbox as $note) {
      echo '<p>';
      echo 'From : ' . htmlspecialchars($note->from) . '<br />';
      echo 'Date : ' . htmlspecialchars($note->date) . '<br />';
      echo 'Time : ' . htmlspecialchars($note->time) . '<br />';
      echo 'Body : ' . htmlspecialchars($note->body) . '<br />';
    
      //now iterate over the child array
      foreach ($note->recipients as $recipient) {
          echo 'x : ' . htmlspecialchars($recipient->address) . '<br />';
          echo 'y : ' . htmlspecialchars($recipient->name) . '<br />';
          echo 'z : ' . htmlspecialchars($recipient->$recipient->address) . '<br />';
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-06
      • 1970-01-01
      • 2019-03-26
      • 2020-05-21
      • 2011-09-20
      • 1970-01-01
      • 2014-09-25
      • 2021-02-18
      • 2021-04-26
      相关资源
      最近更新 更多