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++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -