View Single Post
Old 11-30-2012, 05:04 AM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I wouldn't suggest that doing that is a good idea, although you will typically see issues if you alter the positioning of the pointer or swap locations. Can cause interesting issues when using unset with cyclical lists though. Biggest problem is that the keyword here is *may*. May is the worst to come across as that indicates intermittent which is a nightmare to debug unlike absolute failure.
So short answer, don't manipulate the array in a foreach that would change the internal structure. Setting a new value is fine, but removing one may be problematic. Same goes with adding, use a while loop instead. I would also suggest a while loop for an unset.
For something simple like above, you should instead make use of array_filter to remove what you don't want. It creates a new array with everything that passes the filter.
Fou-Lu is offline   Reply With Quote