While looping through a multidimensional array with foreach(), how do you know which index you are on? Reason being: if you want to search through the multi. array to delete a value, there may be two instances of the same value. So, I figured the index would be unique to the value. I just don't know how to find the value of the index once the foreach() loop has found the item to delete.
Any help would be greatly appreciated!

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.

11-29-2012, 05:17 PM

Custard7A

Quote:

Originally Posted by Fou-Lu

Hisss, don't use unset during a foreach. Altering arrays during traversals such as foreach or using iterators may cause undefined behaviour.

It was meant to remove the files, and the initial "." and ".." from the directory scan. I don't use it at the moment, I'm still wondering if that was bad use of unset (scandir seems a little more predictable, your comment seemed like a catchall).

11-30-2012, 06:04 AM

Fou-Lu

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.

11-30-2012, 10:30 AM

Custard7A

I suppose that's the kind of thing you can do and everything is fine, until a year later you make some changes to the environment it's in and then can't work out why the script is suddenly misbehaving. Problems like that are such a headache. Thanks for the help, I am trying to stop tip-toeing around arrays and get some confidence with using them. :thumbsup: