2

I need to modify looping of foreach loop while its running.

$array = array('a' => 10, 'b' => 20, 'c' => 30);
foreach($array as $key => $value) 
{
    echo $value. " ";
    if($key == 'b')
        $array['d'] = 40;
}
print_r($array);

I get output like 10 20 30 but, I want that to 10 20 30 40

Array was modify inside the loop perfectly and giving result Array ( [a] => 10 [b] => 20 [c] => 30 [d] => 40 )

Problem is foreach can't loop added element.

GuRu
  • 1,880
  • 3
  • 23
  • 32

0 Answers0