Voting

: zero plus zero?
(Example: nine)

The Note You're Voting On

php at omit dot ianco dot co dot uk
17 years ago
I wanted to be able to add to an array while looping through it. foreach does not allow this because it is using a secret copy of the array. each makes this possible (tested on PHP 4).
<?php
$shopping_list
= array('oysters', 'caviare');
reset ($shopping_list);
while (list(
$key, $value) = each ($shopping_list)) {
if (
$value == 'oysters') $shopping_list[] = 'champagne';
elseif (
$value == 'champagne') $shopping_list[] = 'ice';
}
print_r($shopping_list);
// Array ( [0] => oysters [1] => caviare [2] => champagne [3] => ice )
?>

<< Back to user notes page

To Top