php - Looping exceed the array limit -


using php example , there array:

$list = array("a", "b", "c"); 

i nth element of array if infinite loop. example, 11th element here "b":

  b  c   b  c   b  c   b  c ...  1  2  3  4  5  6  7  8  9 10 11 12 ...                                ^ 

how loop on array this?

simply use % modulus

<?php  $list = array("a", "b", "c"); $position = 11; $index = $position % count($list) - 1;  echo $list[$index]; 

demo : https://eval.in/847933


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -