Selecting every nth item from an array

后端 未结 8 1953
春和景丽
春和景丽 2021-02-02 13:41

What would be the most efficient way to select every nth item from a large array? Is there a \'smart\' way to do it or is looping the only way?

Some points to c

8条回答
  •  萌比男神i
    2021-02-02 14:13

    You can't move the array pointer, it seems, more than once at a time. I would personally use this:

    reset($source);
    $next = true;
    while($next === true){
        $result[] = current($source);
        for(i=0;i<205;i++){
            $next = next($source);
        }
    }
    

    If someone can find a function that can move the array pointer more than just one step at a time, you will have a better answer. I think this is good though.

提交回复
热议问题