PHP:循环 - 如何添加最大项目
PHP: looping - how to add max items
我正在使用下面的脚本根据主题标签显示来自 Instagram 的图片。它工作完美,并列出了 Instagram 可用的所有图像 - 最多 20 张图像。
但是,我希望能够显示更少的图片,比如说 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>";
}
?>
如果你想显示 1-12 张图像而其他不显示则使用计数器。在此代码中,如果计数器大于 12,则循环将中断,因此只有 1-12 图像将显示
$count = 0;
foreach ($json->entry_data->TagPage{0}->tag->media->nodes AS $item) {
if($count >= 12){
break;
}
echo "<div class='imgbox'><a href='http://instagram.com/p/".$item->code."' target='_blank'><img class='hashtag' src='" . $item->display_src . "' alt=''></a></div>";
$count++;
}
简单。使用计数器变量。
$loop_count = 0;
$max = 12;
foreach ($things as $thing) {
if ($loop_count >= $max) {
break;
}
// Do loop logic here
$loop_count++;
}
使用 for
循环:
for ($i = 0; $i < 12; $i++) {
$item = $json->entry_data->TagPage{0}->tag->media->nodes[$i];
echo "<div class='imgbox'><a href='http://instagram.com/p/".$item->code."' target='_blank'><img class='hashtag' src='" . $item->display_src . "' alt=''></a></div>";
}
如果一个数组是数字索引那么这个工作
foreach ( $list as $index => $value ) {
if ( $index > 12 ) break;
//do something
}
在您的案例中实现如下:
<?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 $index => $item) {
if ( $index > 12 ) break;
echo "<div class='imgbox'><a href='http://instagram.com/p/".$item->code."' target='_blank'><img class='hashtag' src='" . $item->display_src . "' alt=''></a></div>";
}
?>
我正在使用下面的脚本根据主题标签显示来自 Instagram 的图片。它工作完美,并列出了 Instagram 可用的所有图像 - 最多 20 张图像。
但是,我希望能够显示更少的图片,比如说 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>";
}
?>
如果你想显示 1-12 张图像而其他不显示则使用计数器。在此代码中,如果计数器大于 12,则循环将中断,因此只有 1-12 图像将显示
$count = 0;
foreach ($json->entry_data->TagPage{0}->tag->media->nodes AS $item) {
if($count >= 12){
break;
}
echo "<div class='imgbox'><a href='http://instagram.com/p/".$item->code."' target='_blank'><img class='hashtag' src='" . $item->display_src . "' alt=''></a></div>";
$count++;
}
简单。使用计数器变量。
$loop_count = 0;
$max = 12;
foreach ($things as $thing) {
if ($loop_count >= $max) {
break;
}
// Do loop logic here
$loop_count++;
}
使用 for
循环:
for ($i = 0; $i < 12; $i++) {
$item = $json->entry_data->TagPage{0}->tag->media->nodes[$i];
echo "<div class='imgbox'><a href='http://instagram.com/p/".$item->code."' target='_blank'><img class='hashtag' src='" . $item->display_src . "' alt=''></a></div>";
}
如果一个数组是数字索引那么这个工作
foreach ( $list as $index => $value ) {
if ( $index > 12 ) break;
//do something
}
在您的案例中实现如下:
<?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 $index => $item) {
if ( $index > 12 ) break;
echo "<div class='imgbox'><a href='http://instagram.com/p/".$item->code."' target='_blank'><img class='hashtag' src='" . $item->display_src . "' alt=''></a></div>";
}
?>