# [ACCEPTED]-Selecting every nth item from an array-arrays

Score: 17

A foreach loop provides the fastest iteration 12 over your large array based on comparison 11 testing. I'd stick with something similar 10 to what you have unless somebody wishes 9 to solve the problem with loop unrolling.

This answer should 8 run quicker.

``````\$result = array();
\$i = 0;
foreach(\$source as \$value) {
if (\$i++ % 205 == 0) {
\$result[] = \$value;
}
}
``````

I don't have time to test, but 7 you might be able to use a variation of 6 @haim's solution if you first numerically 5 index the array. It's worth trying to see 4 if you can receive any gains over my previous 3 solution:

``````\$result = array();
\$source = array_values(\$source);
\$count = count(\$source);
for(\$i = 0; \$i < \$count; \$i += 205) {
\$result[] = \$source[\$i];
}
``````

This would largely depend on how 2 optimized the function array_values is. It 1 could very well perform horribly.

Score: 8

Also, using one the new Spl datastructures might yield 1 better results than using plain arrays.

Score: 6

I recommend to using `array_slice`

``````\$count = count(\$array) ;
for(\$i=205;\$i<\$count;\$i+=205){
\$result[] = array_slice(\$array,\$i,1);
}
``````

If your array was numerically 1 indexed, this would be very fast:

``````\$count = count(\$array) ;
for(\$i=205;\$i<\$count;\$i+=205){
\$result[] = \$array[\$i];
}
``````
Score: 2

I think the solution to this problem doesn't 22 lie in any PHP syntax but in the design 21 of your code.

You could either make the array 20 numerically indexed (might not be plausible 19 for your application), keep track of every 18 205th item, or only search the array once 17 (cache a list of each 205th item).

In my 16 mind, keeping track of each 205th item would 15 be easier to implement. You'd just keep 14 a count of all the items in a database or 13 something, and every time an item is added, check 12 the modulo of the count. If you have another 11 205th item, add it to the array. For when 10 items are deleted though, this would be 9 trickier. You might have to re-check the 8 entire array to realign all your 205th items.

Doing 7 this would be simpler if you could start 6 at the deleted item and move forward, but 5 again this would only work for numerically 4 indexed arrays -- and if that were true, you 3 wouldn't have to move forward at all, you 2 would just do a little maths to re-figure 1 it out.

• Numerical indexes -- better long-term solution but harder to implement
• Keeping track -- easier to implement, but you'd have to get dirty again when you delete items
• Caching items -- you should probably do this for the other two solutions as well, but on its own, it would be fast until the array were modified, in which case you would probably have to re-do it.
Score: 1

If this really is a bottleneck, you might 5 want to consider rethinking your design 4 in order to make it numerically indexed.

EDIT: Or 3 create and maintain a seperate array with 2 just the 205th items (which gets updated 1 at insert or something like that).

Score: 0

You can't move the array pointer, it seems, more 5 than once at a time. I would personally 4 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 3 that can move the array pointer more than 2 just one step at a time, you will have a 1 better answer. I think this is good though.

Score: 0
• Create a two dimentional array [205][N]
• Access 205th element for every N

May sound silly but by definition it is 2 fastest since you access memory locations 1 directly and do not perform any comparisons.

Score: 0

This Code can select all the even/odd indexed element from an array

``````<?php
\$a = [1 => 'One', 2 => 'Two', 3 => 'Three', 4 => 'Four', 7 => 'Seven', 8 => 'Eight'];
echo "<br>";
foreach (\$a as \$key => \$value) {

if (\$key % 2 == 0) {
echo \$value . ' ';
}
}
``````

0

More Related questions