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

如何使用foreach PHP忽略前4个值并输出rest-after-then值,php,mysql,variables,foreach,Php,Mysql,Variables,Foreach,脚本的第一部分从数组中获取前4个值,并将结果作为列表输出。脚本的第二部分应获取其余的值,如5、6。。10等,并将它们全部放在一个div中,但目前脚本的第二部分仍然从第一部分开始获取数组的所有值 这是我的剧本 $counter = 0; foreach ($value as $category_topic) if(++$counter <= 4){ $template_image = '<img src="'.$image_path.$category_t

脚本的第一部分从数组中获取前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=0;$i<4;$i++)
{
//your code...
}
第二部分

for($i=4;$i<count($value);$i++)
{
//your code...
}
我可能会按照建议使用,但这里有一个替代方案:

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
}

谢谢,但在剧本的哪一部分,是上还是下?@AlexB为什么不两者都选?或者让我换一种说法:您希望从指定索引开始的每个部分
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
}