dongpang1232 2013-05-12 11:50
浏览 11
已采纳

循环4数组元素。 然后休息。 然后循环4个下一个数组元素,依此类推,直到数组结束

My array contains a dynamic amount of element. they are grouped in 4. so

array
 [0] -> "group one"
 [1] -> "group one"
 [2] -> "group one"
 [3] -> "group one"

 [4] -> "group two"
 [5] -> "group two"
 [6] -> "group two"
 [7] -> "group two"

 [8] -> "group three"
 [9] -> "group three"
 [10] -> "group three"
 [11] -> "group three"

What I want to do is output and test all the elements i each group in one loop, add then do the same with the next group.

I suspect it should be easy.. using a counter to keep track of how many outputs etc, but apparently my brain wont work properly on this problem :)

Thanks to anyone who bothers helping me :)

  • 写回答

2条回答 默认 最新

  • dongqiao1151 2013-05-12 11:58
    关注

    Take a look at array_chunk.

    <?php
    foreach(array_chunk(range(1, 12), 4) as $index => $chunk) {
        echo "Printing Chunk $index
    ";
        foreach($chunk as $number) {
            echo "$number
    ";
        }
    }
    
    /*
        Printing Chunk 0
        1
        2
        3
        4
        Printing Chunk 1
        5
        6
        7
        8
        Printing Chunk 2
        9
        10
        11
        12
    */
    

    Anthony.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角