Skip to content
Advertisement

get value 1 – 2 – 1 – 2 – 1 in for loop php

I am trying to get value like 1-2-1-2-1-2 from for loop

I tried code like this

$c = count($response['video']); // 2
$currentPage  = 2;
$k = 1;
for ($i=$k; $i <= $currentPage ; $i++) {
    $b = $i > $c ? $k : $i;
    $urls = $response['video'][$b - 1];
    echo "$b";
}

where $currentPage = 5; $c =2;

when $c = 3; the value should be 1-2-3-1-2-3 when i echo $b or dd($b) out of loop i will got 12111 but i need answer 121212 please help me to solve this.

Advertisement

Answer

Is this what you want?

$k = 1;
$currentPage = 5;
$c = 3;
for ($b=$i=$k; $i <= $currentPage ; $i++, $b++) {
    $b = $b <= $c ? $b : 1;
    echo $b;
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement