Revision: 31106
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at August 30, 2010 06:09 by mjsiemer
Initial Code
This is not possible because ‘foreach’ operates on a copy of the array so there is no way to do it, don’t waste your time :) BUT You can work around this by replacing the ‘foreach’ with a ‘while’ loop, but before you do so, you must know that the following loops are functionally identical: foreach ($days as $day) { echo $day; } while (list(, $day) = each($days)) { echo $day; } The same is true for these two, they are functionally identical: foreach ($days as $i => $day) { echo $i .': ' .$day; } while (list($i, $day) = each($days)) { echo $i .': ' .$day; } ‘While’ used this way with ‘each’ doesn’t operate on a copy of the array so you can do something like this, replace your ‘foreach’ with a ‘while’ loop and: while (list(, $token) = each($tokens)) { /* Skip white spaces */ if ($token == ' ') { while ($token == ' ') $token = next($tokens); } }
Initial URL
http://codingrecipes.com/php-advancing-array-pointer-in-a-foreach-loop
Initial Description
Initial Title
Advancing pointer in a foreach loop
Initial Tags
php
Initial Language
PHP