Is it safe to remove an element from an array during a foreach?

If you want to loop through the array and remover multiple items then yes, it could mess you up a little since you’ll be changing what items are at which index. You’ll have to create some additional logic to account for this.

You could use a for loop with break, and then decrement the index each time you remove an item so that you don’t end up skipping items.