我有以下数组
$example=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
$limit=4 // 4 at the beginning only...
//it used to get incremented automatically to 8,12,16....
首先,我希望1,2,3,4
作为我已完成的输出
foreach($example as $eg)
{
if($eg>$limit)
continue;
}
我很容易在第一个1,2,3,4
然后1,2,3,4,5,6,7,8
1,2,3,4,5,6,7,8,9,10,11,12
但是现在我想要的是1,2,3,4
一开始5,6,7,8
然后9,10,11,12
lyk this ...我怎么能得到那个???
请帮帮我... :)
AS
foreach($example as $eg)
{
if($eg>$limit)
continue;
}
仅返回1,2,3,4
的{{1}}和$limit=4
的{{1}}
我需要1,2,3,4,5,6,7,8
的{{1}}和$limit=8
的{{1}}
答案 0 :(得分:3)
您是否尝试过使用有用的内置功能?
array_chunk($example,$limit);
或者,对于更多类似页面的行为:
$pagenum = 2; // change based on page
$offset = $pagenum * $limit;
array_slice($example,$offset,$limit);
答案 1 :(得分:1)
你首先会对数组进行分块,这样每个chunk都有4个元素,然后循环遍历每个chunk:
要根据哪个组更改显示的数字,您可以执行以下操作:
$group = $_GET['group'];
$items = array_chunk($example, ceil(count($example)/4)[$group-1];
echo implode(", ", $items);
然后你可以去
yoursite.com/page.php?group=1
它会输出
1, 2, 3, 4
当你去
yoursite.com/page.php?group=2
将输出
5, 6, 7, 8
等
答案 2 :(得分:1)
你可以尝试这样的事情
$example=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
$limit = 8;
$limit -= 4;
for($i = $limit; $i < ($limit + 4); $i++)
{
echo $example[$i].' ';
}
<强>输出强>
//for $limit 4 output 1 2 3 4
//for $limit 8 output 5 6 7 8
//for $limit 16 output 13 14 15 16