我正在使用下面的腳本來顯示來自Instagram的基於標籤的圖像。它完美地工作,並列出了Instagram提供的所有圖像 - 最多20個圖像。PHP:循環 - 如何添加最大項目
但是,我希望能夠少顯示10或12張圖像。
如何添加某種變量來保存最大項目值,以便foreach
循環不循環所有項目?
PHP:
<?php
// Enter hashtag;
$hashtag = "nofilter";
$url = "https://www.instagram.com/explore/tags/".$hashtag."/";
$instagram_content = file_get_contents($url);
preg_match_all('/window._sharedData = (.*)\;\<\/script\>/', $instagram_content, $matches);
$txt = implode('', $matches[1]);
$json = json_decode($txt);
foreach ($json->entry_data->TagPage{0}->tag->media->nodes AS $item) {
echo "<div class='imgbox'><a href='http://instagram.com/p/".$item->code."' target='_blank'><img class='hashtag' src='" . $item->display_src . "' alt=''></a></div>";
}
?>
「某種變數」就像一個國家之三? – j08691
是的,如: $ noOfImages = 12; - 然後foreach只會遍歷12張圖片。 – Meek