I am trying to solve a problem where I have an array of days like this
$days = array ('Monday','Tuesday ','Wednesday ','Thursday ','friday');
I want to print them out 66 times in a loop, and everytime it gets to Friday the array reset itself and print Monday. I tried this:
$days = current($days);
while($days <= 66){
echo $days;
next ($days);
if (end ($days)){
reset($days);
}
<?php
$days = array ('Monday','Tuesday ','Wednesday ','Thursday ','friday');
for($i=0 ; $i<66; $i++ ){
foreach ($days as $row){
echo $row;
echo '</br>';
}
}
You just need 2 loops. 1 for the day array and one for the times you want to repeat.
$num = 0;
for ($i=1;$i<=66;$i++){
print_r($days[$num]);
$num = ($num ==5) ? 0 : num +1;
}
Have two counter variables, and do something like this.
$a = 0;
$days = array('Monday','Tuesday ','Wednesday ','Thursday ','friday');
for ($i = 0; $i < 65; $i++) {
print($days[$a]);
$a++;
if ($a == sizeof($days)) {
$a = 0;
}
}
This is a fantastic opportunity to use the extensive range of Iterator classes available in the SPL
<?php
$days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
$infinite = new InfiniteIterator(new ArrayIterator($days));
foreach ( new LimitIterator($infinite, 0, 66) as $value ) {
echo $value, PHP_EOL;
}