php - how to fetch next value in foreach loop -
i have foreach
loop below code :
foreach($coupons $k=>$c ){ //... }
now, fetch 2 values in every loop .
for example :
first loop: 0,1
second loop: 2,3
third loop: 4,5
how can ?
split array chunks of size 2
:
$chunks = array_chunk($coupons, 2); foreach ($chunks $chunk) { if (2 == sizeof($chunk)) { echo $chunk[0] . ',' . $chunk[1]; } else { // if last chunk contains 1 element echo $chunk[0]; } }
if want preserve keys - use third parameter true
:
$chunks = array_chunk($coupons, 2, true); print_r($chunks);
Comments
Post a Comment