PHP:循环-如何添加最大项

PHP:循环-如何添加最大项,php,instagram,Php,Instagram,我使用下面的脚本显示Instagram中基于标签的图像。它工作得很好,列出了Instagram可以提供的所有图像,最多20幅 然而,我希望能够显示更少,比如说10或12张图片 如何添加某种变量来保存最大项值,以便foreach循环不会循环所有项 PHP: 如果要显示1-12个图像和其他图像,请不要使用计数器。在这段代码中,若计数器大于12,那个么循环将中断,所以只显示1-12个图像 $count = 0; foreach ($json->entry_data->TagPage{0

我使用下面的脚本显示Instagram中基于标签的图像。它工作得很好,列出了Instagram可以提供的所有图像,最多20幅

然而,我希望能够显示更少,比如说10或12张图片

如何添加某种变量来保存最大项值,以便
foreach
循环不会循环所有项

PHP:


如果要显示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++;     
 }
$count=0;
foreach($json->entry_data->TagPage{0}->tag->media->nodes AS$item){
如果($count>=12){
打破
}
回声“;
$count++;
}

如果要显示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++;     
 }
$count=0;
foreach($json->entry_data->TagPage{0}->tag->media->nodes AS$item){
如果($count>=12){
打破
}
回声“;
$count++;
}

简单。使用计数器变量

$loop_count = 0;
$max = 12;
foreach ($things as $thing) {
    if ($loop_count >= $max) {
        break;
    }

    // Do loop logic here

    $loop_count++;
}

简单。使用计数器变量

$loop_count = 0;
$max = 12;
foreach ($things as $thing) {
    if ($loop_count >= $max) {
        break;
    }

    // Do loop logic here

    $loop_count++;
}

循环使用

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>";
}
($i=0;$i<12;$i++)的
{
$item=$json->entry_data->TagPage{0}->tag->media->nodes[$i];
回声“;
}

使用
循环:

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>";
}
($i=0;$i<12;$i++)的
{
$item=$json->entry_data->TagPage{0}->tag->media->nodes[$i];
回声“;
}

如果一个数组是数字索引的,那么这个工作

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>";
    }
  ?>

如果数组是数字索引的,则此工作

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>";
    }
  ?>

“某种变量”比如计数器?是的,比如:$noOfImages=12;-然后foreach将只循环12个图像。“某种变量”比如计数器?是的,比如:$noOfImages=12;-然后foreach只会循环12幅图像,就像这样。非常感谢。非常感谢。谢谢。