hi I have an array lets say the array has 3 elements: $array[] = 'myvalue1'; $array[] = 'myvalue2'; $array[] = 'myvalue3'; now I have for loop which has 10 loops: for($i=0; $i<10; $i++) { echo $array[$i]; } what I want is to pick a number from the given array each time the loop executes. in this way: myvalue1 picked in first loop execution. myvalue2 picked in second loop execution. myvalue3 picked in third loop execution. now I want to go back and pick myvalue1 again in the forth loop.. and again in fifth loop it should pick myvalue2 and so on.. I've been trying to do it for 1 day. and no luck..
The thing he showed you is called modulo (%) for($i=0; $i<10; $i++) { echo $array[($i%3)]; } PHP: I would use it like that or for code that´s easier to read like: for($i=0; $i<10; $i++) { $i = $i%3; echo $array[$i]; } PHP:
Use modulo (%) operator i%3 returns i if i<3 and it returns 0 when i=3 s that the value of index will always be 0,1,or 2. You can print indefinite times the array values. for($i=0; $i<10; $i++) { $i = $i%3; echo $array[$i]; } PHP: