Selecting every nth item from an array

后端 未结 8 1987
春和景丽
春和景丽 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条回答
  •  情书的邮戳
    2021-02-02 14:25

    You could use array_keys to work only on the array keys.

    $keys = array_keys($array);
    for ($i=0, $n=min(count($keys), 130000); $i<$n; $i += 205) {
        $result[] = $array[$keys[$i]];
    }
    

提交回复
热议问题