0

I have a loop that needs to go through each item. So naturally a foreach loop seems like the best idea. However, I need to add an element to the array as it iterates. I tried the following without any luck.

foreach ($allitems as $item) {
    //Do some stuff here

    if ($value === true)
        $allitems[] = 'New item';
}

I found out the foreach loops seem to use a referenced copy of the array, so editing the array does not register in the loop.

A workaround is to use the older styled while loops as follows:

while (list($key, $item) = each($allitems)) {
    //Do some stuff here

    if ($value === true)
        $allitems[] = 'New item';
}

Clearly a foreach loop would be nicer and more efficient. Is it possible? Or is the while structure the best possible solution.

3 Answers 3

3

Yeah, it is possible:

foreach ($allitems as &$item) {
    //Do some stuff here

    if ($value === true)
        $allitems[] = 'New item';
}

According to the docs, you need to pass a reference (using the & in front if $item)

More concrete example:

<?php
$allitems = array(1,2,3,4);

foreach ($allitems as &$item) {
     echo $item."\n";
     if ($item == 2) {
          $allitems[] = "Blah";
     }
}
?>

This outputs (using php from commandline)

1
2
3
4
Blah
Sign up to request clarification or add additional context in comments.

5 Comments

Im fairly sure i've tried that without luck. It did successfully append to the array. However the foreach was not able to access the new elements. I was only able to access them afterwards.
tried your example and it works, so I played around a bit. Turns out it was due to the fact that I was calling the append as I was on the last iteration of my array ($item == 4 in the example). I read earlier that foreach increments then executes. So the loop assumed it was done.
@Foo_Chow Doesn't that mean that this solution won't always work, since you won't process elements that are added on the last iteration? Or is that not necessary in your application?
@Barmar All that needed to be done was add a dummy element to the original array in my case. array(1,2,3,4,NULL); in the case of the example
But when you add elements, they'll be after the dummy element. What happens if you add an element based on the last new element?
1

It seems like an ordinary for loop would be best for this:

for ($i = 0; $i < count($array); $i++) {
  // Do some stuff here that calculates $value from $array[$i]
  if ($value === true) {
    $array[] = "New Element";
  }
}

Comments

0

You could do foreach...like this....

But it adds more code...so its not any better than your while loop..

foreach($array as $val){
  if($val=="check"){$append[]="New Item";}
}
$array = array_merge($array, $append);

Of course, if you want your structure maintained..then rather use array_push

3 Comments

shouldnt the array_merge be inside the foreach?
No, because you loop, build the new array, and when you're done....merge them together
Ahh possibly I wasnt too clear. My target is to loop through the new elements as they are added to the foreach array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.