How to use round robin method in array in php?

Hi, I am trying to create an additional array from array.ie; I think I have an array like the one below
$array = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19} 

which I explode and assigns it to the variable $ i .. and run the for loop as shown below.

 for ( $i=0;$i<count($array);$i++) { $a = array(); $b = $array[$i]; for($j=0;$j<count($array);$j++){ if($b != $array[$j]){ $a[] = $array[$j]; } } 

The output I want is when

 $i = 1 

the array should be

 {2,3,4,5,6,7,8,9,10,11} 

and when

 $i = 2 

the array should be

 {3,4,5,6,7,8,9,10,11,12} 

similarly when

 $i=19 

the array should be

 {1,2,3,4,5,6,7,8,9,10} 

so how can i do that.

+5
source share
6 answers

Assuming $i assumed to be an offset rather than the actual value in the array, you can do

 $fullArray = range(1, 19); $i = 19; $valuesToReturn = 10; $subset = iterator_to_array( new LimitIterator( new InfiniteIterator( new ArrayIterator($fullArray) ), $i, $valuesToReturn ) ); print_r($subset); 

This will give you the desired result, for example.

 $i = 1 will give 2 to 11 $i = 2 will give 3 to 12 … $i = 10 will give 11 to 1 $i = 11 will give 12 to 2 … $i = 19 will give 1 to 10 $i = 20 will give the same as $i = 1 again 

etc.

+5
source
 for ($i = 0; $i < count($array); $i++) { if ($i + 10 < count($array)) $a = array_slice($array, $i, 10); else $a = array_merge(array_slice($array, $i), array_slice($array, 0, 10-(count($array)-$i))); // do something with $a before it is over-written on the next iteration } 

This test:

 <?php $array = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19); for ($i = 0; $i < count($array); $i++) { if ($i + 10 < count($array)) $a = array_slice($array, $i, 10); else $a = array_merge(array_slice($array, $i), array_slice($array, 0, 10-(count($array)-$i))); echo "<h2>$i</h2>\n<pre>".print_r($a,true)."</pre><br />\n"; } 

As a result:

 0 Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 ) ... 9 Array ( [0] => 10 [1] => 11 [2] => 12 [3] => 13 [4] => 14 [5] => 15 [6] => 16 [7] => 17 [8] => 18 [9] => 19 ) 10 Array ( [0] => 11 [1] => 12 [2] => 13 [3] => 14 [4] => 15 [5] => 16 [6] => 17 [7] => 18 [8] => 19 [9] => 1 ) ... 18 Array ( [0] => 19 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 [9] => 9 ) 
+2
source

It works fine from my end

 <?php $array = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19); $size = sizeof($array); // Defining the array size $str = 17; // This is the reference value from which you have to extract the values $key = array_search($str, $array); $key = $key+1; // in order to skip the given reference value $start = $key%$size; $end = $start+9; for($i=$start; $i<=$end; $i++) { $j = ($i%$size); $result[] = $array[$j]; } echo '<pre>'; print_r($result); ?> 
+2
source
 $array = range(1, 19); $i = 19; $result = array(); $after = array_slice($array, $i, 10); $before = array_slice($array, 0, 10 - count($after)); $result = array_merge($after, $before); var_dump(json_encode($result)); 

PS note that element 0 has 1 value, etc.

+2
source

It seems that all you need is a fragment of a certain size from the array, a slice of which is wrapped around the end of the array and continues from the very beginning. It treats the array as a circular list.

You can achieve this in many ways, one of the easiest (in terms of lines of code) is to expand the original array by adding a copy at the end and using the PHP array_slice() function to extract the required fragment:

 function getWrappedSlice(array $array, $start, $count = 10) { return array_slice(array_merge($array, $array), $start, $count); } 

Of course, you must be sure that $start is between 0 and count($array) - 1 (including), otherwise the value returned by the function will not be what you expect.

+1
source

Round-robin on the array can be achieved by performing a rotation operation within each iteration:

 for ($i = 0; $i < count($array); ++$i) { // rotate the array (left) array_push($array, array_shift($array)); // use $array } 

During the loop, the first element of the array is placed at the back. At the end of the loop, the array returns to its original value.

+1
source

Source: https://habr.com/ru/post/1214143/


All Articles