php - preview next array element using foreach -


i have foreach iterates between items in array build excel file. want place total row after each item in array if next element in array matches specific condition (has specific value in 1 key). want use next() check value far know , read here using next i'll move pointer next row , answer use current. how can see next item in array without moving pointer? reading question have written this:

foreach($creds $cred){     echo $cred['tipo'];     //if next item has 'tipo' key specific value     if(current($creds['tipo'])!='4'){         echo 'total';     } } 

but not working. how fix it?

edit: based on answers move echo 'total' stuff @ beginning of next iteration instead of @ bottom of current iteration. there no other solutions using foreach far see in answers

the requirement is:

  • inside foreach loop @ next value or key not change position of current iterator pointer of foreach loop.

although more code, 1 way create separate iterator on same array.

this new iterator has kept in step foreach loop quite simple do.

the advantage can manipulate way wish using next(), prev(), current() etc. , know foreach loop not affected.

for example requirement test next entry ahead of th current one. worthwhile starting iterator pointing second entry , advance each time.

example, untested code...

$iter = new \arrayiterator($cred)s; // mirror of `foreach` loop  

iterator foreach($creds $cred) {

    // next key , value...     $iter->next();      $nextkey = $iter->key();     $nextvalue = $iter->current();      echo $cred['tipo'];      // if next item has 'tipo' key specific value     if($nextvalue['tipo']) != '4'){         echo 'total';     } } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -