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

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -