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>';
精彩评论