I am trying to add a key => value pair to an array when using the foreach loop, when this value is added, the foreach loop should process the new key pair =>.
$array = array( 'one' => 1, 'two' => 2, 'three' => 3 ); foreach($array as $key => $value) { if ($key == 'three') { $array['four'] = 4; } else if ($key == 'four') { $array['five'] = 5; } }
If I print an array after the loop, I would expect to see all 5 squares, but instead I only see this:
Array ( [one] => 1 [two] => 2 [three] => 3 [four] => 4 )
Is there some way when I add a fourth pair to actually process it so that the fifth pair is added to this foreach loop (or another type of loop?)
source share