1

I loop through an array, which I have, using a foreach loop. However within the foreach loop I need to modify the array, so that it directly affects my foreach loop.

So I will make an example of my problem:

<?php

    $array = ["Red", "Yellow", "Blue", "Orange"];

    foreach($array as $color) {
        if(($key = array_search("Blue", $array)) !== false) 
            unset($array[$key]);

        echo $color . "<br>";

    }

?>

output:

Red
Yellow
Blue
Orange

So as you can see I unset() the array element with the value Blue. But I still have it in my output.

Now my question is: How can I unset the element with the value Blue, so that it directly affects my foreach loop, means I won't see it anymore in the output, since I deleted it before I loop over that specific element.

Expected output would be (Note: Blue is not in the output):

Red
Yellow
Orange
Rizier123
  • 58,877
  • 16
  • 101
  • 156
user4676307
  • 409
  • 8
  • 22

1 Answers1

3

You could assign your array by reference to another variable, so that is_ref is 1, means the foreach loop doesn't loop over a copy of your array anymore.

So just put this before your foreach loop:

$arr = &$array;

For more information how foreach actually works see: How does PHP 'foreach' actually work?

Also note, since there are some changes in PHP 7: http://php.net/manual/en/migration70.incompatible.php#migration70.incompatible.foreach this won't work anymore in PHP 7.

If you want to do the same in PHP 7 where the behavior is changed just say that you want to loop through the array by reference, e.g.

foreach($array as &$color)
Community
  • 1
  • 1
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • thank you for your answer I tried to do what you advised me but I is not working for me. I have updated the question to make it more clear please read my edits. – user4676307 Sep 01 '15 at 13:57
  • @user4676307 Also if you need to do this for the inner array, assign the other array by reference to a variable. – Rizier123 Sep 01 '15 at 14:08
  • Thank for your help and I can see it working. Can you please guide me with what exactly I need to do as this is really confusing to me and I have never used is_ref before. So I have have a main array 'exported_skus'. Then I need to create another array $a = $exported_skus and the $b = &a . Is this right please – user4676307 Sep 01 '15 at 14:26
  • @user4676307 if you don't want to loop over a copy for: `$exported_skus`, just do: `$xy = &$exported_skus;` before the foreach. If you want to do this for: `$related_products` then do the same thing just with another variable name, but also before the **first** foreach. (You only need one ref) – Rizier123 Sep 01 '15 at 14:28
  • I have added $arr = &$exported_skus; just before my first foreach. Now when deleting elements do I delete from $arr or $export_skus. I tried to by deleting from $exported_skus and it did't work. – user4676307 Sep 01 '15 at 14:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88491/discussion-between-user4676307-and-rizier123). – user4676307 Sep 01 '15 at 14:32