note 99909 modified in control-structures.foreach by danbrown

From: Date: Tue, 14 Sep 2010 15:14:45 +0000
Subject: note 99909 modified in control-structures.foreach by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-172548@lists.php.net to get a copy of this message
For those who'd like to traverse an array including just added elements (within this very foreach), here's a workaround: <?php $values = array(1 => 'a', 2 => 'b', 3 => 'c'); while (list($key, $value) = each($values)) { echo "$key => $value \r\n"; if ($key == 3) { $values[4] = 'd'; } if ($key == 4) { $values[5] = 'e'; } } ?> the code above will output: 1 => a 2 => b 3 => c 4 => d 5 => e --was-- For those who'd like to traverse an array including just added elements (within this very foreach), here's a workaround: <? $values = array(1 => 'a', 2 => 'b', 3 => 'c'); while (list($key, $value) = each($values)) { echo "$key => $value \r\n"; if ($key == 3) { $values[4] = 'd'; } if ($key == 4) { $values[5] = 'e'; } } ?> the code above will output: 1 => a 2 => b 3 => c 4 => d 5 => e http://php.net/manual/en/control-structures.foreach.php

« previous php.notes (#172548) next »