Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-28-2012, 08:14 PM   PM User | #1
wojo1086
Regular Coder

 
Join Date: Mar 2010
Location: Orlando, FL
Posts: 153
Thanks: 2
Thanked 8 Times in 8 Posts
wojo1086 is an unknown quantity at this point
Question 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!
wojo1086 is offline   Reply With Quote
Old 11-28-2012, 08:38 PM   PM User | #2
davidjones1990
New Coder

 
Join Date: Sep 2011
Posts: 22
Thanks: 0
Thanked 3 Times in 3 Posts
davidjones1990 is an unknown quantity at this point
You can use unset () to remove a item from a array

http://php.net/manual/en/function.unset.php
davidjones1990 is offline   Reply With Quote
Old 11-28-2012, 09:13 PM   PM User | #3
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
PHP Code:
foreach ($array as $key=>$value) {
    
// do stuff

$key is the index of each $array element
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 11-28-2012, 09:29 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,659
Thanks: 4
Thanked 2,452 Times in 2,421 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
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.
Fou-Lu is offline   Reply With Quote
Old 11-29-2012, 04:17 PM   PM User | #5
Custard7A
Regular Coder

 
Custard7A's Avatar
 
Join Date: Jul 2010
Location: Australia
Posts: 269
Thanks: 32
Thanked 32 Times in 32 Posts
Custard7A is an unknown quantity at this point
Quote:
Originally Posted by Fou-Lu View Post
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).
Custard7A is offline   Reply With Quote
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,659
Thanks: 4
Thanked 2,452 Times in 2,421 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
Old 11-30-2012, 09:30 AM   PM User | #7
Custard7A
Regular Coder

 
Custard7A's Avatar
 
Join Date: Jul 2010
Location: Australia
Posts: 269
Thanks: 32
Thanked 32 Times in 32 Posts
Custard7A is an unknown quantity at this point
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.
Custard7A is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


Advertisement
Log in to turn off these ads.