Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中,如何在foreach循环中获得8结果后的计数?_Php_Loops_Foreach - Fatal编程技术网

在php中,如何在foreach循环中获得8结果后的计数?

在php中,如何在foreach循环中获得8结果后的计数?,php,loops,foreach,Php,Loops,Foreach,我想在一张幻灯片中显示8个结果,其他结果将在下一张幻灯片中显示。我已经通过以下代码完成了这项工作 $counter = 0; foreach ($products as $product) { if(++$counter % 8 === 0) { $firstSlide = ($counter == 1) ? 'active' : ''; $slideItem = '<div class

我想在一张幻灯片中显示8个结果,其他结果将在下一张幻灯片中显示。我已经通过以下代码完成了这项工作

$counter = 0;
            foreach ($products as $product) {
if(++$counter % 8 === 0) {
                    $firstSlide = ($counter == 1) ? 'active' : '';
                    $slideItem = '<div class="item  ' . $firstSlide . ' "><div class="fill"></div>';
                    echo $slideItem;
                } 
}
$array      =   array(1,2,3,1,123,123,234,56,67,); // this is your actual array
$arrayChunk =   array_chunk($array, 8);
foreach($arrayChunk as $arrayNum=>$chunkArray) {
    if(is_array($chunkArray)) {
        foreach ($chunkArray as $value) {
            print $value.' ';
        }
    }
    print '<br> slide '.$arrayNum.'<br>';
}
到目前为止,我已经搜索了,但找不到任何解决方案,如果有人知道如何实现,请指导我。

请使用此代码

$counter = 1;
foreach ($products as $product) {
    if($counter % 8 == 0) {
    $firstSlide = ($counter % 8 == 1) ? 'active' : '';
    $slideItem = '<div class="item  ' . $firstSlide . ' "><div class="fill"></div></div>';
    echo $slideItem;
    $counter++;
    } 
}
$counter=1;
foreach($products as$product){
如果($counter%8==0){
$firstSlide=($counter%8==1)?“活动”:“”;
$slideItem='';
echo$slideItem;
$counter++;
} 
}
使用以下代码

$counter = 0;
            foreach ($products as $product) {
if(++$counter % 8 === 0) {
                    $firstSlide = ($counter == 1) ? 'active' : '';
                    $slideItem = '<div class="item  ' . $firstSlide . ' "><div class="fill"></div>';
                    echo $slideItem;
                } 
}
$array      =   array(1,2,3,1,123,123,234,56,67,); // this is your actual array
$arrayChunk =   array_chunk($array, 8);
foreach($arrayChunk as $arrayNum=>$chunkArray) {
    if(is_array($chunkArray)) {
        foreach ($chunkArray as $value) {
            print $value.' ';
        }
    }
    print '<br> slide '.$arrayNum.'<br>';
}
$array=array(1,2,3,11231232334,56,67,);//这是您的实际阵列
$arrayChunk=array\u chunk($array,8);
foreach($arrayChunk作为$arrayNum=>$chunkArray){
if(is_数组($chunkArray)){
foreach($chunkArray作为$value){
打印$value。“;
}
}
打印“
幻灯片”。$arrayNum.
”; }
看一看,你好像忘了带一个
$slideItem@yolenoyer是的,我只是忘了这里,但我已经在我的文件中添加了您的意思是要显示幻灯片
1
,幻灯片
2
,。。?