How to access N-th element of an array in PHP

后端 未结 3 905
误落风尘
误落风尘 2021-02-07 11:36

I\'m embarrassed to ask this and it\'s most likely a duplicate, but my google results are coming up short (im searching incorrectly I guess) and such a basic question is infuria

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-07 12:19

    If you want to access every N-th element:

    $n = 3;
    foreach (array_keys($arr) as $i => $key) {
        if (($i+1) % $n) {
            continue;
        }
    
        $value = $arr[$key];
    }
    

提交回复
热议问题