Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Mar 2010
    Location
    Orlando, FL
    Posts
    161
    Thanks
    4
    Thanked 8 Times in 8 Posts

    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!

  • #2
    New Coder
    Join Date
    Sep 2011
    Posts
    74
    Thanks
    0
    Thanked 7 Times in 7 Posts
    You can use unset () to remove a item from a array

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

  • #3
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    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

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #5
    Regular Coder Custard7A's Avatar
    Join Date
    Jul 2010
    Location
    Australia
    Posts
    286
    Thanks
    32
    Thanked 33 Times in 33 Posts
    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).

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.

  • #7
    Regular Coder Custard7A's Avatar
    Join Date
    Jul 2010
    Location
    Australia
    Posts
    286
    Thanks
    32
    Thanked 33 Times in 33 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •