|
Hisss, don't use unset during a foreach. Altering arrays during traversals such as foreach or using iterators may cause undefined behaviour. Typically it is fine, but intermittent issues may occur. While would be a better option, but IMO you shouldn't alter it unless you are pushing and popping the stacks. You can also get the internal pointer location of any array item by pulling the key() off of the item in question (ie: print key($myArray);). Key works on the array level, not on an item within the array.
If its treated as an array with incrementing indexes, use a for loop instead. If you are looking for duplicate values within an array, you can use the array_keys method with the second option to search which will return an array of keys matching the value. These can then be unset successfully outside of a traversal. If you don't care about which duplicates are handled, you can simply use array_unique to dump any duplicates.
|