Answer the question
In order to leave comments, you need to log in
How to iterate over a simple array of days of the week?
Good afternoon,
straight to the question) it’s hard to call it a question, and yet it caused problems ...
And so there is an array of days [1,2,3,4,5,6,7];
I have an interval from 3-2 i.e. it turns out includes all days of the week
I need to iterate over the array c 3 to 2 value, tell me how to do it without ifs) and pasta code)
for($i=1; $i<=7;$++){
}
Answer the question
In order to leave comments, you need to log in
Absolutely without if it will not work probably:
$n = 3;
$m = 2;
for ($i = $n; ; $i = ($i < 7 ? $i + 1 : 1)){
echo $i . ' ';
if ($i == $m) break;
}
$n = 3;
$m = 2;
$i = $n - 1;
do {
$d = $i % 7 + 1;
echo $d . ' ';
$i++;
} while ($d != $m);
$array = [1, 2, 3, 4, 5, 6, 7];
$n = 3;
$m = 2;
$i = array_search($n, $array);
$result = array_merge(array_slice($array, $i), array_slice($array, 0, $i));
3 to 2 is in reverse order, right? Then:
$reversed = array_reverse($array);
for($i=4; $i<6; $i++){
echo $reversed[$i];
}
I don't quite understand what you want, but if
need to loop through the array c 3 to 2 value
for($i=3; $i>=2;$i--){
echo "{$i}-";
}
need to iterate over this case like this 3-4-5-6-7-1-2
foreach (array(3,4,5,6,7,1,2) as $i) {
echo "{$i}-";
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question