Peek ahead when iterating an array in PHP 5.2
Posted
by pako
on Stack Overflow
See other posts from Stack Overflow
or by pako
Published on 2010-03-16T21:00:55Z
Indexed on
2010/03/16
21:31 UTC
Read the original article
Hit count: 264
Is it possible to "peek ahead" while iterating an array in PHP 5.2? For example, I often use foreach to manipulate data from an array:
foreach($array as $object) {
// do something
}
But I often need to peek at the next element while going through the array. I know I could use a for
loop and reference the next item by it's index ($array[$i+1]
), but it wouldn't work for associative arrays. Is there any elegant solution for my problem, perhaps involving SPL?
© Stack Overflow or respective owner