CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   foreach()-How to find out which index number you're on? (http://www.codingforums.com/showthread.php?t=283165)

wojo1086 11-28-2012 08:14 PM

foreach()-How to find out which index number you're on?
 
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!

davidjones1990 11-28-2012 08:38 PM

You can use unset () to remove a item from a array

http://php.net/manual/en/function.unset.php

AndrewGSW 11-28-2012 09:13 PM

PHP Code:

foreach ($array as $key=>$value) {
    
// do stuff


$key is the index of each $array element

Fou-Lu 11-28-2012 09:29 PM

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.

Custard7A 11-29-2012 04:17 PM

Quote:

Originally Posted by Fou-Lu (Post 1295684)
Hisss, don't use unset during a foreach. Altering arrays during traversals such as foreach or using iterators may cause undefined behaviour.

Would you say that in all cases?

I remember playing around with something or other like this..

PHP Code:



$array 
scandir($dir);

foreach(
$array as $key => $value) { if(strchr($value'.') { unset($key); }} 

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).

Fou-Lu 11-30-2012 05:04 AM

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.

Custard7A 11-30-2012 09:30 AM

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:


All times are GMT +1. The time now is 09:44 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.