如何使用foreach PHP忽略前4个值并输出rest after then值


How to ignore first 4 and output rest after then values using foreach PHP

脚本的第一部分从数组中获取前4个值,并将结果作为列表输出。脚本的第二部分应该获得其余的值,如5、6。。10,等等,并将它们全部放在一个div中,但目前我的脚本第二部分仍然从第一个开始获取数组的所有值。

这是我的脚本

    $counter = 0;
    foreach ($value as $category_topic)
    if(++$counter <= 4){
   $template_image = '<img src="'.$image_path.$category_topic['ImagePath'].DS.$category_topic['templateImage'].'" width="'.$category_topic['templimgwidth'].'" height="'.$category_topic['templimgheight'].'" alt="'.$category_topic['templateTitle'].'" title="'.$category_topic['templateTitle'].'">';
    $template_link ='<a href="'.DST.$category_topic['ImagePath'].DS.$category_topic['referring_url'].'">'.$category_topic['templateTitle'].'</a>';
    print<<<END
      <li>
       <ul>
        <li>{$template_image}</li>
        <li class="bot_link">{$template_link}</li>
       </ul>
      </li>
    END;
    }
    print <<<END
    </ul>
    <div>
    END;
    foreach ($value as $category_topic)
    if(++$counter > 4){
    $template_link[++$counter] ='<a href="'.DST.$category_topic['ImagePath'].DS.$category_topic['referring_url'].'">'.$category_topic['templateTitle'].'</a>';
    print<<<END
    {$template_link}
    END;
    }
    print <<<END
    </div>
    </div>
    END;

使用for而不是foreach

for($i = 4; $i < count($value); $i++)
{
  // your code...... 
}

编辑:

对于第一部分。

for($i=0;$i<4;$i++)
{
//your code...
}

用于第二部分。

for($i=4;$i<count($value);$i++)
{
//your code...
}

我可能会按照建议使用for,但这里有一个替代方案:

foreach(array_slice($value, 0, 4) as $category_topic) {
    //...this is the first 4
}
//...
foreach(array_slice($value, 4) as $category_topic) {
    //...this is 5 to the end
}