doulianxing4015 2014-03-05 13:50
浏览 122
已采纳

foreach()每4个变化输出

I have an issue where I don't know for a foreach() loop to change the output on every(x) amount of results.

Here is my foreach() code:

$dir_handle = 'assets/icons/';
    foreach(array_diff(scandir($dir_handle), array('.', '..')) as $file) {
        $cut = substr($file, -4);
        echo '<a href="action.php?do=changeicon&set=' . $cut . '"><img id="preload_header" src="assets/icons/' . $file . '" /></a><br />';
}

How would I get it for that 1-4 have the same result, but then 5-8 have a different result, and then back to 1-4?

  • 写回答

2条回答 默认 最新

  • dongsi1944 2014-03-05 13:57
    关注

    You want to do a count in your foreach loop

    $count = 1;
    foreach(array_diff(scandir($dir_handle), array('.', '..')) as $file) {
        //Check if count is between 1 and 4
        if($count >= 1 && $count <= 4) {
    
            //Do something
    
        } else { //Otherwise it must be between 5 and 8
    
            //Do something else
    
            //If we are at 8 go back to one otherwise just increase the count by 1
            if($count == 8) {
                $count = 1;
            } else {
                $count++;
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法