开发者

php json decode how to select and show one foreach items? not all?

foreach ($data as $result) { 
  echo '<div class="title"><a href="'.htmlspecialchars($result->link).'">'.htmlspecialchars($result->description).'</a></div>';
  if(!empty($result->attachment->media)){
    foreach ($result->attachment->media as $media) {
      echo '<div class="image"><a href="'.htmlspecialchars($media->{0}->href).'"><img src="'.htmlspecialchars($media->{0}->src).'" /></a></div>'; 
    }
  }
}

I have used this php json decode to get some data. in t开发者_开发知识库he second foreach part foreach ($result->attachment->media as $media), it returned 4 foreach items, but I just need the second one, how to select and show the foreach items?


If you just want to show the second attachment, if it's an array , refer to it via it's index:

  $result->attachment->media[1]

Which would give you:

echo '<div class="image"><a href="'.htmlspecialchars($result->attachment->media[1]->{0}->href).'"><img src="'.htmlspecialchars($result->attachment->media[1]->{0}->src).'" /></a></div>';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜