safe to delete elements of array in foreach

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Slaughter

    safe to delete elements of array in foreach

    Is it safe to remove elements from an array that foreach is working on?
    (normally this is not the case but not sure in php) If so is there an
    efficient way to handle it? (I could add the indexes to a temp array and
    delete afterwards if necessary but since I'm actually working in a nested
    situation this could get a little messy. I guess I could set there values to
    null and remove them afterwards?

    Thanks,
    Jon


  • ZeldorBlat

    #2
    Re: safe to delete elements of array in foreach

    On May 18, 11:05 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
    wrote:
    Is it safe to remove elements from an array that foreach is working on?
    (normally this is not the case but not sure in php) If so is there an
    efficient way to handle it? (I could add the indexes to a temp array and
    delete afterwards if necessary but since I'm actually working in a nested
    situation this could get a little messy. I guess I could set there values to
    null and remove them afterwards?
    >
    Thanks,
    Jon
    Why don't you try it and see what happens?

    Comment

    • Jon Slaughter

      #3
      Re: safe to delete elements of array in foreach


      "ZeldorBlat " <zeldorblat@gma il.comwrote in message
      news:1179501018 .125313.63600@p 77g2000hsh.goog legroups.com...
      On May 18, 11:05 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
      wrote:
      >Is it safe to remove elements from an array that foreach is working on?
      >(normally this is not the case but not sure in php) If so is there an
      >efficient way to handle it? (I could add the indexes to a temp array and
      >delete afterwards if necessary but since I'm actually working in a nested
      >situation this could get a little messy. I guess I could set there values
      >to
      >null and remove them afterwards?
      >>
      >Thanks,
      >Jon
      >
      Why don't you try it and see what happens?
      >
      Um... cause I did... but that doesn't mean much. Just cause someone tries
      something doesn't prove that it will always work like that...

      got any more bright ideas?

      Or is the question to hard for you?


      Comment

      • ZeldorBlat

        #4
        Re: safe to delete elements of array in foreach

        On May 18, 11:40 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
        wrote:
        "ZeldorBlat " <zeldorb...@gma il.comwrote in message
        >
        news:1179501018 .125313.63600@p 77g2000hsh.goog legroups.com...
        >
        On May 18, 11:05 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
        wrote:
        Is it safe to remove elements from an array that foreach is working on?
        (normally this is not the case but not sure in php) If so is there an
        efficient way to handle it? (I could add the indexes to a temp array and
        delete afterwards if necessary but since I'm actually working in a nested
        situation this could get a little messy. I guess I could set there values
        to
        null and remove them afterwards?
        >
        Thanks,
        Jon
        >
        Why don't you try it and see what happens?
        >
        Um... cause I did... but that doesn't mean much. Just cause someone tries
        something doesn't prove that it will always work like that...
        >
        got any more bright ideas?
        >
        Or is the question to hard for you?
        No, the question is not to (sic) hard for me. But, as you've already
        discovered, it isn't that difficult to test, either.

        Comment

        • Jerry Stuckle

          #5
          Re: safe to delete elements of array in foreach

          ZeldorBlat wrote:
          On May 18, 11:40 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
          wrote:
          >"ZeldorBlat " <zeldorb...@gma il.comwrote in message
          >>
          >news:117950101 8.125313.63600@ p77g2000hsh.goo glegroups.com.. .
          >>
          >>On May 18, 11:05 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
          >>wrote:
          >>>Is it safe to remove elements from an array that foreach is working on?
          >>>(normally this is not the case but not sure in php) If so is there an
          >>>efficient way to handle it? (I could add the indexes to a temp array and
          >>>delete afterwards if necessary but since I'm actually working in a nested
          >>>situation this could get a little messy. I guess I could set there values
          >>>to
          >>>null and remove them afterwards?
          >>>Thanks,
          >>>Jon
          >>Why don't you try it and see what happens?
          >Um... cause I did... but that doesn't mean much. Just cause someone tries
          >something doesn't prove that it will always work like that...
          >>
          >got any more bright ideas?
          >>
          >Or is the question to hard for you?
          >
          No, the question is not to (sic) hard for me. But, as you've already
          discovered, it isn't that difficult to test, either.
          >
          Sorry, I agree with Jon on this one.

          I make it a habit not to delete entries in a foreach() loop. Rather, I
          build an array of keys I want to delete, and after the loop ends, delete
          the entries from my delete array.

          I don't know whether an operation like this is guaranteed to work in PHP
          - I've never seen it documented, so I suspect not. And just because it
          works in one release under a certain set of conditions is not a
          guarantee it will work on another release or under different conditions.


          --
          =============== ===
          Remove the "x" from my email address
          Jerry Stuckle
          JDS Computer Training Corp.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          • ZeldorBlat

            #6
            Re: safe to delete elements of array in foreach

            On May 18, 9:05 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
            ZeldorBlat wrote:
            On May 18, 11:40 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
            wrote:
            "ZeldorBlat " <zeldorb...@gma il.comwrote in message
            >
            >news:117950101 8.125313.63600@ p77g2000hsh.goo glegroups.com.. .
            >
            >On May 18, 11:05 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
            >wrote:
            >>Is it safe to remove elements from an array that foreach is working on?
            >>(normally this is not the case but not sure in php) If so is there an
            >>efficient way to handle it? (I could add the indexes to a temp array and
            >>delete afterwards if necessary but since I'm actually working in a nested
            >>situation this could get a little messy. I guess I could set there values
            >>to
            >>null and remove them afterwards?
            >>Thanks,
            >>Jon
            >Why don't you try it and see what happens?
            Um... cause I did... but that doesn't mean much. Just cause someone tries
            something doesn't prove that it will always work like that...
            >
            got any more bright ideas?
            >
            Or is the question to hard for you?
            >
            No, the question is not to (sic) hard for me. But, as you've already
            discovered, it isn't that difficult to test, either.
            >
            Sorry, I agree with Jon on this one.
            >
            I make it a habit not to delete entries in a foreach() loop. Rather, I
            build an array of keys I want to delete, and after the loop ends, delete
            the entries from my delete array.
            >
            I don't know whether an operation like this is guaranteed to work in PHP
            - I've never seen it documented, so I suspect not. And just because it
            works in one release under a certain set of conditions is not a
            guarantee it will work on another release or under different conditions.
            >
            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstuck...@attgl obal.net
            =============== ===
            I never said I disagreed with him -- in fact I, too, generally don't
            delete elements inside a foreach. However, I will say that when I
            have done it things seem to work as expected. I guess it all comes
            down to whether or not the array's internal pointer is modified when
            you unset the element it's pointing to (I suspect it isn't).

            I see a lot of questions in these newsgroups that look something like,
            "What happens if I do X?" or "In PHP is this code valid?" The point I
            was trying to make (and apparently Jon took offense to it) was that
            it's easy enough to just try it and see what happens. Software is
            just that: soft. It can be changed easily enough :)


            Comment

            • Jerry Stuckle

              #7
              Re: safe to delete elements of array in foreach

              ZeldorBlat wrote:
              On May 18, 9:05 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
              >ZeldorBlat wrote:
              >>On May 18, 11:40 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
              >>wrote:
              >>>"ZeldorBla t" <zeldorb...@gma il.comwrote in message
              >>>news:1179501 018.125313.6360 0@p77g2000hsh.g ooglegroups.com ...
              >>>>On May 18, 11:05 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
              >>>>wrote:
              >>>>>Is it safe to remove elements from an array that foreach is working on?
              >>>>>(normall y this is not the case but not sure in php) If so is there an
              >>>>>efficien t way to handle it? (I could add the indexes to a temp array and
              >>>>>delete afterwards if necessary but since I'm actually working in a nested
              >>>>>situatio n this could get a little messy. I guess I could set there values
              >>>>>to
              >>>>>null and remove them afterwards?
              >>>>>Thanks,
              >>>>>Jon
              >>>>Why don't you try it and see what happens?
              >>>Um... cause I did... but that doesn't mean much. Just cause someone tries
              >>>something doesn't prove that it will always work like that...
              >>>got any more bright ideas?
              >>>Or is the question to hard for you?
              >>No, the question is not to (sic) hard for me. But, as you've already
              >>discovered, it isn't that difficult to test, either.
              >Sorry, I agree with Jon on this one.
              >>
              >I make it a habit not to delete entries in a foreach() loop. Rather, I
              >build an array of keys I want to delete, and after the loop ends, delete
              >the entries from my delete array.
              >>
              >I don't know whether an operation like this is guaranteed to work in PHP
              >- I've never seen it documented, so I suspect not. And just because it
              >works in one release under a certain set of conditions is not a
              >guarantee it will work on another release or under different conditions.
              >>
              >--
              >============== ====
              >Remove the "x" from my email address
              >Jerry Stuckle
              >JDS Computer Training Corp.
              >jstuck...@attg lobal.net
              >============== ====
              >
              I never said I disagreed with him -- in fact I, too, generally don't
              delete elements inside a foreach. However, I will say that when I
              have done it things seem to work as expected. I guess it all comes
              down to whether or not the array's internal pointer is modified when
              you unset the element it's pointing to (I suspect it isn't).
              >
              I see a lot of questions in these newsgroups that look something like,
              "What happens if I do X?" or "In PHP is this code valid?" The point I
              was trying to make (and apparently Jon took offense to it) was that
              it's easy enough to just try it and see what happens. Software is
              just that: soft. It can be changed easily enough :)
              >
              >
              Yes, and in a case like this that change can break his code.

              As I said - I've never seen it documented that this is valid. Maybe it
              is and I missed it; I really don't know.

              But this isn't the same as a lot of other "try it and find out"
              questions. In this case it's a known problem in other programming
              languages, and if it is documented that this should or should not work,
              no one has pointed anyone to it.

              And until I see something from the PHP developers saying it is OK, I
              wouldn't do it.

              --
              =============== ===
              Remove the "x" from my email address
              Jerry Stuckle
              JDS Computer Training Corp.
              jstucklex@attgl obal.net
              =============== ===

              Comment

              • ZeldorBlat

                #8
                Re: safe to delete elements of array in foreach

                On May 18, 10:44 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                ZeldorBlat wrote:
                On May 18, 9:05 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                ZeldorBlat wrote:
                >On May 18, 11:40 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
                >wrote:
                >>"ZeldorBlat " <zeldorb...@gma il.comwrote in message
                >>>news:1179501 018.125313.6360 0@p77g2000hsh.g ooglegroups.com ...
                >>>On May 18, 11:05 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
                >>>wrote:
                >>>>Is it safe to remove elements from an array that foreach is working on?
                >>>>(normally this is not the case but not sure in php) If so is there an
                >>>>efficient way to handle it? (I could add the indexes to a temp array and
                >>>>delete afterwards if necessary but since I'm actually working in a nested
                >>>>situation this could get a little messy. I guess I could set there values
                >>>>to
                >>>>null and remove them afterwards?
                >>>>Thanks,
                >>>>Jon
                >>>Why don't you try it and see what happens?
                >>Um... cause I did... but that doesn't mean much. Just cause someone tries
                >>something doesn't prove that it will always work like that...
                >>got any more bright ideas?
                >>Or is the question to hard for you?
                >No, the question is not to (sic) hard for me. But, as you've already
                >discovered, it isn't that difficult to test, either.
                Sorry, I agree with Jon on this one.
                >
                I make it a habit not to delete entries in a foreach() loop. Rather, I
                build an array of keys I want to delete, and after the loop ends, delete
                the entries from my delete array.
                >
                I don't know whether an operation like this is guaranteed to work in PHP
                - I've never seen it documented, so I suspect not. And just because it
                works in one release under a certain set of conditions is not a
                guarantee it will work on another release or under different conditions.
                >
                --
                =============== ===
                Remove the "x" from my email address
                Jerry Stuckle
                JDS Computer Training Corp.
                jstuck...@attgl obal.net
                =============== ===
                >
                I never said I disagreed with him -- in fact I, too, generally don't
                delete elements inside a foreach. However, I will say that when I
                have done it things seem to work as expected. I guess it all comes
                down to whether or not the array's internal pointer is modified when
                you unset the element it's pointing to (I suspect it isn't).
                >
                I see a lot of questions in these newsgroups that look something like,
                "What happens if I do X?" or "In PHP is this code valid?" The point I
                was trying to make (and apparently Jon took offense to it) was that
                it's easy enough to just try it and see what happens. Software is
                just that: soft. It can be changed easily enough :)
                >
                Yes, and in a case like this that change can break his code.
                >
                As I said - I've never seen it documented that this is valid. Maybe it
                is and I missed it; I really don't know.
                >
                But this isn't the same as a lot of other "try it and find out"
                questions. In this case it's a known problem in other programming
                languages, and if it is documented that this should or should not work,
                no one has pointed anyone to it.
                >
                And until I see something from the PHP developers saying it is OK, I
                wouldn't do it.
                >
                --
                =============== ===
                Remove the "x" from my email address
                Jerry Stuckle
                JDS Computer Training Corp.
                jstuck...@attgl obal.net
                =============== ===
                This suggests that it's safe (from <http://www.php.net/foreach>):

                "Unless the array is referenced, foreach operates on a copy of the
                specified array and not the array itself. Therefore, the array pointer
                is not modified as with the each() construct, and changes to the array
                element returned are not reflected in the original array."

                So unsetting a value in the original array should not affect the copy
                that foreach is working on.

                Comment

                • Rami Elomaa

                  #9
                  Re: safe to delete elements of array in foreach

                  Jon Slaughter kirjoitti:
                  Is it safe to remove elements from an array that foreach is working on?
                  (normally this is not the case but not sure in php) If so is there an
                  efficient way to handle it? (I could add the indexes to a temp array and
                  delete afterwards if necessary but since I'm actually working in a nested
                  situation this could get a little messy. I guess I could set there values to
                  null and remove them afterwards?
                  $a = range(0,10);
                  foreach($a as $key =$val)
                  if($val%2)
                  unset($a[$key]);

                  I've done something like this and have never had any problems. I don't
                  see the threats in this kind of thing, can you explain me what you think
                  that might be unsafe in this method?

                  --
                  Rami.Elomaa@gma il.com

                  "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
                  usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

                  Comment

                  • Schraalhans Keukenmeester

                    #10
                    Re: safe to delete elements of array in foreach

                    At Fri, 18 May 2007 21:05:36 -0400, Jerry Stuckle let his monkeys type:
                    ZeldorBlat wrote:
                    >On May 18, 11:40 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
                    >wrote:
                    >>"ZeldorBlat " <zeldorb...@gma il.comwrote in message
                    >>>
                    >>news:11795010 18.125313.63600 @p77g2000hsh.go oglegroups.com. ..
                    >>>
                    >>>On May 18, 11:05 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
                    >>>wrote:
                    >>>>Is it safe to remove elements from an array that foreach is working on?
                    >>>>(normally this is not the case but not sure in php) If so is there an
                    >>>>efficient way to handle it? (I could add the indexes to a temp array and
                    >>>>delete afterwards if necessary but since I'm actually working in a nested
                    >>>>situation this could get a little messy. I guess I could set there values
                    >>>>to
                    >>>>null and remove them afterwards?
                    >>>>Thanks,
                    >>>>Jon
                    >>>Why don't you try it and see what happens?
                    >>Um... cause I did... but that doesn't mean much. Just cause someone tries
                    >>something doesn't prove that it will always work like that...
                    >>>
                    >>got any more bright ideas?
                    >>>
                    >>Or is the question to hard for you?
                    >>
                    >No, the question is not to (sic) hard for me. But, as you've already
                    >discovered, it isn't that difficult to test, either.
                    >>
                    >
                    Sorry, I agree with Jon on this one.
                    >
                    I make it a habit not to delete entries in a foreach() loop. Rather, I
                    build an array of keys I want to delete, and after the loop ends, delete
                    the entries from my delete array.
                    >
                    I don't know whether an operation like this is guaranteed to work in PHP
                    - I've never seen it documented, so I suspect not. And just because it
                    works in one release under a certain set of conditions is not a
                    guarantee it will work on another release or under different conditions.
                    I agree 100% with you not to rely on undocumented 'features'.
                    Just out of curiosity I took this one step further and discovered the
                    following (again, can't rely on this to hold true unless it's documented
                    somewhere, well hidden):

                    <?PHP
                    $array=array('j ohn','james','d elilah','mary') ;
                    foreach ($array as $key=>$value) {
                    echo "$key =$value".NEWLIN E;
                    if ($value == 'delilah') {
                    $array[$key]='samson';
                    }
                    elseif ($value=='james ') {
                    unset($array[$key]);
                    }
                    }
                    echo NEWLINE;
                    foreach ($array as $key=>$value) {
                    echo "$key =$value".NEWLIN E;
                    }
                    ?>
                    0 =john
                    1 =james
                    2 =delilah
                    3 =mary

                    0 =john
                    2 =samson
                    3 =mary

                    The foreach loop operates on a copy of the array
                    The original array remains in scope
                    Current($array) points to the first element throughout the loop

                    I'd think setting elements to NULL directly in the array isn't any better,
                    you're still relying on the same 'feature'.

                    Sh

                    Comment

                    • Schraalhans Keukenmeester

                      #11
                      Re: safe to delete elements of array in foreach

                      At Fri, 18 May 2007 20:10:57 -0700, ZeldorBlat let his monkeys type:
                      >============== ====
                      >
                      This suggests that it's safe (from <http://www.php.net/foreach>):
                      >
                      "Unless the array is referenced, foreach operates on a copy of the
                      specified array and not the array itself. Therefore, the array pointer
                      is not modified as with the each() construct, and changes to the array
                      element returned are not reflected in the original array."
                      >
                      Okay, well, uhmmm, not so well hidden then.

                      Try unsetting and/or changing values in a referenced array foreach:
                      You might not end u with what you expect. I repeated the code in my other
                      reply, now operating on $value directly:

                      $array=array('j ohn','james','d elilah','mary') ;
                      foreach ($array as $key=>&$value) {
                      if ($value == 'delilah') {
                      $value ='samson';
                      }
                      elseif ($value=='james ') {
                      $value = null;;
                      }
                      echo "$key =$value".NEWLIN E;
                      }

                      foreach ($array as $key=>$value) {
                      echo "$key =$value".NEWLIN E;
                      }
                      Output:
                      0 =john
                      1 =>
                      2 =samson
                      3 =mary
                      0 =john
                      1 =>
                      2 =samson
                      3 =samson // !!!

                      Must be some logic for this, but I fail to see it right now.

                      Sh.

                      Comment

                      • Jon Slaughter

                        #12
                        Re: safe to delete elements of array in foreach


                        "ZeldorBlat " <zeldorblat@gma il.comwrote in message
                        news:1179544257 .218638.219550@ u30g2000hsc.goo glegroups.com.. .
                        On May 18, 10:44 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                        >ZeldorBlat wrote:
                        On May 18, 9:05 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                        >ZeldorBlat wrote:
                        >>On May 18, 11:40 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
                        >>wrote:
                        >>>"ZeldorBla t" <zeldorb...@gma il.comwrote in message
                        >>>>news:117950 1018.125313.636 00@p77g2000hsh. googlegroups.co m...
                        >>>>On May 18, 11:05 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
                        >>>>wrote:
                        >>>>>Is it safe to remove elements from an array that foreach is
                        >>>>>working on?
                        >>>>>(normall y this is not the case but not sure in php) If so is there
                        >>>>>an
                        >>>>>efficien t way to handle it? (I could add the indexes to a temp
                        >>>>>array and
                        >>>>>delete afterwards if necessary but since I'm actually working in a
                        >>>>>nested
                        >>>>>situatio n this could get a little messy. I guess I could set there
                        >>>>>values
                        >>>>>to
                        >>>>>null and remove them afterwards?
                        >>>>>Thanks,
                        >>>>>Jon
                        >>>>Why don't you try it and see what happens?
                        >>>Um... cause I did... but that doesn't mean much. Just cause someone
                        >>>tries
                        >>>something doesn't prove that it will always work like that...
                        >>>got any more bright ideas?
                        >>>Or is the question to hard for you?
                        >>No, the question is not to (sic) hard for me. But, as you've already
                        >>discovered, it isn't that difficult to test, either.
                        >Sorry, I agree with Jon on this one.
                        >>
                        >I make it a habit not to delete entries in a foreach() loop. Rather,
                        >I
                        >build an array of keys I want to delete, and after the loop ends,
                        >delete
                        >the entries from my delete array.
                        >>
                        >I don't know whether an operation like this is guaranteed to work in
                        >PHP
                        >- I've never seen it documented, so I suspect not. And just because
                        >it
                        >works in one release under a certain set of conditions is not a
                        >guarantee it will work on another release or under different
                        >conditions.
                        >>
                        >--
                        >============== ====
                        >Remove the "x" from my email address
                        >Jerry Stuckle
                        >JDS Computer Training Corp.
                        >jstuck...@attg lobal.net
                        >============== ====
                        >>
                        I never said I disagreed with him -- in fact I, too, generally don't
                        delete elements inside a foreach. However, I will say that when I
                        have done it things seem to work as expected. I guess it all comes
                        down to whether or not the array's internal pointer is modified when
                        you unset the element it's pointing to (I suspect it isn't).
                        >>
                        I see a lot of questions in these newsgroups that look something like,
                        "What happens if I do X?" or "In PHP is this code valid?" The point I
                        was trying to make (and apparently Jon took offense to it) was that
                        it's easy enough to just try it and see what happens. Software is
                        just that: soft. It can be changed easily enough :)
                        >>
                        >Yes, and in a case like this that change can break his code.
                        >>
                        >As I said - I've never seen it documented that this is valid. Maybe it
                        >is and I missed it; I really don't know.
                        >>
                        >But this isn't the same as a lot of other "try it and find out"
                        >questions. In this case it's a known problem in other programming
                        >languages, and if it is documented that this should or should not work,
                        >no one has pointed anyone to it.
                        >>
                        >And until I see something from the PHP developers saying it is OK, I
                        >wouldn't do it.
                        >>
                        >--
                        >============== ====
                        >Remove the "x" from my email address
                        >Jerry Stuckle
                        >JDS Computer Training Corp.
                        >jstuck...@attg lobal.net
                        >============== ====
                        >
                        This suggests that it's safe (from <http://www.php.net/foreach>):
                        >
                        "Unless the array is referenced, foreach operates on a copy of the
                        specified array and not the array itself. Therefore, the array pointer
                        is not modified as with the each() construct, and changes to the array
                        element returned are not reflected in the original array."
                        >
                        So unsetting a value in the original array should not affect the copy
                        that foreach is working on.

                        Your right. I saw this after the fact after I did try it out and compared it
                        with a for loop. The foreach worked while the for loop didn't. Then I went
                        to the manual and saw that it worked on a copy.

                        So in this case it should work just fine. The for looped version actually
                        doesn't work and does give the problem I suspected. (thought I sent a post
                        a bout it though).

                        So unless they go change how foreach works in a newer version I think it
                        will be safe. I'm still trying to get used to php as it does a lot of things
                        differently than I'm used to.

                        The issue with "trying it out" is that in things like this you can't be sure
                        since its a implementation artifact... The only way to know is to know that
                        foreach is working on a copy. Of course maybe one could realize that by
                        doing a few examples but when usually one doesn't want to rely on what they
                        think is going on in this sorta situation and needs to know the facts.

                        Of course now I know its completely safe which was my original question ;)
                        It came from the manual which is where I should have looked in the fist
                        place(I did but I guess I skipped over that part ;/

                        Thanks,
                        Jon


                        Comment

                        • Jerry Stuckle

                          #13
                          Re: safe to delete elements of array in foreach

                          ZeldorBlat wrote:
                          On May 18, 10:44 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                          >ZeldorBlat wrote:
                          >>On May 18, 9:05 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                          >>>ZeldorBlat wrote:
                          >>>>On May 18, 11:40 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
                          >>>>wrote:
                          >>>>>"ZeldorBla t" <zeldorb...@gma il.comwrote in message
                          >>>>>news:11795 01018.125313.63 600@p77g2000hsh .googlegroups.c om...
                          >>>>>>On May 18, 11:05 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
                          >>>>>>wrote:
                          >>>>>>>Is it safe to remove elements from an array that foreach is working on?
                          >>>>>>>(normall y this is not the case but not sure in php) If so is there an
                          >>>>>>>efficien t way to handle it? (I could add the indexes to a temp array and
                          >>>>>>>delete afterwards if necessary but since I'm actually working in a nested
                          >>>>>>>situatio n this could get a little messy. I guess I could set there values
                          >>>>>>>to
                          >>>>>>>null and remove them afterwards?
                          >>>>>>>Thanks ,
                          >>>>>>>Jon
                          >>>>>>Why don't you try it and see what happens?
                          >>>>>Um... cause I did... but that doesn't mean much. Just cause someone tries
                          >>>>>somethin g doesn't prove that it will always work like that...
                          >>>>>got any more bright ideas?
                          >>>>>Or is the question to hard for you?
                          >>>>No, the question is not to (sic) hard for me. But, as you've already
                          >>>>discovere d, it isn't that difficult to test, either.
                          >>>Sorry, I agree with Jon on this one.
                          >>>I make it a habit not to delete entries in a foreach() loop. Rather, I
                          >>>build an array of keys I want to delete, and after the loop ends, delete
                          >>>the entries from my delete array.
                          >>>I don't know whether an operation like this is guaranteed to work in PHP
                          >>>- I've never seen it documented, so I suspect not. And just because it
                          >>>works in one release under a certain set of conditions is not a
                          >>>guarantee it will work on another release or under different conditions.
                          >>>--
                          >>>============ ======
                          >>>Remove the "x" from my email address
                          >>>Jerry Stuckle
                          >>>JDS Computer Training Corp.
                          >>>jstuck...@at tglobal.net
                          >>>============ ======
                          >>I never said I disagreed with him -- in fact I, too, generally don't
                          >>delete elements inside a foreach. However, I will say that when I
                          >>have done it things seem to work as expected. I guess it all comes
                          >>down to whether or not the array's internal pointer is modified when
                          >>you unset the element it's pointing to (I suspect it isn't).
                          >>I see a lot of questions in these newsgroups that look something like,
                          >>"What happens if I do X?" or "In PHP is this code valid?" The point I
                          >>was trying to make (and apparently Jon took offense to it) was that
                          >>it's easy enough to just try it and see what happens. Software is
                          >>just that: soft. It can be changed easily enough :)
                          >Yes, and in a case like this that change can break his code.
                          >>
                          >As I said - I've never seen it documented that this is valid. Maybe it
                          >is and I missed it; I really don't know.
                          >>
                          >But this isn't the same as a lot of other "try it and find out"
                          >questions. In this case it's a known problem in other programming
                          >languages, and if it is documented that this should or should not work,
                          >no one has pointed anyone to it.
                          >>
                          >And until I see something from the PHP developers saying it is OK, I
                          >wouldn't do it.
                          >>
                          >--
                          >============== ====
                          >Remove the "x" from my email address
                          >Jerry Stuckle
                          >JDS Computer Training Corp.
                          >jstuck...@attg lobal.net
                          >============== ====
                          >
                          This suggests that it's safe (from <http://www.php.net/foreach>):
                          >
                          "Unless the array is referenced, foreach operates on a copy of the
                          specified array and not the array itself. Therefore, the array pointer
                          is not modified as with the each() construct, and changes to the array
                          element returned are not reflected in the original array."
                          >
                          So unsetting a value in the original array should not affect the copy
                          that foreach is working on.
                          >
                          I read that as just the opposite - changes in the copy do not affect the
                          original array.

                          It doesn't say how often the copy is refreshed from the original array -
                          or when changes in the original are reflected in the copy.

                          --
                          =============== ===
                          Remove the "x" from my email address
                          Jerry Stuckle
                          JDS Computer Training Corp.
                          jstucklex@attgl obal.net
                          =============== ===

                          Comment

                          • Jerry Stuckle

                            #14
                            Re: safe to delete elements of array in foreach

                            Jon Slaughter wrote:
                            "ZeldorBlat " <zeldorblat@gma il.comwrote in message
                            news:1179544257 .218638.219550@ u30g2000hsc.goo glegroups.com.. .
                            >On May 18, 10:44 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                            >>ZeldorBlat wrote:
                            >>>On May 18, 9:05 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                            >>>>ZeldorBla t wrote:
                            >>>>>On May 18, 11:40 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
                            >>>>>wrote:
                            >>>>>>"ZeldorBl at" <zeldorb...@gma il.comwrote in message
                            >>>>>>news:1179 501018.125313.6 3600@p77g2000hs h.googlegroups. com...
                            >>>>>>>On May 18, 11:05 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
                            >>>>>>>wrote:
                            >>>>>>>>Is it safe to remove elements from an array that foreach is
                            >>>>>>>>worki ng on?
                            >>>>>>>>(normal ly this is not the case but not sure in php) If so is there
                            >>>>>>>>an
                            >>>>>>>>efficie nt way to handle it? (I could add the indexes to a temp
                            >>>>>>>>array and
                            >>>>>>>>delet e afterwards if necessary but since I'm actually working in a
                            >>>>>>>>neste d
                            >>>>>>>>situati on this could get a little messy. I guess I could set there
                            >>>>>>>>value s
                            >>>>>>>>to
                            >>>>>>>>null and remove them afterwards?
                            >>>>>>>>Thank s,
                            >>>>>>>>Jon
                            >>>>>>>Why don't you try it and see what happens?
                            >>>>>>Um... cause I did... but that doesn't mean much. Just cause someone
                            >>>>>>tries
                            >>>>>>somethi ng doesn't prove that it will always work like that...
                            >>>>>>got any more bright ideas?
                            >>>>>>Or is the question to hard for you?
                            >>>>>No, the question is not to (sic) hard for me. But, as you've already
                            >>>>>discovered , it isn't that difficult to test, either.
                            >>>>Sorry, I agree with Jon on this one.
                            >>>>I make it a habit not to delete entries in a foreach() loop. Rather,
                            >>>>I
                            >>>>build an array of keys I want to delete, and after the loop ends,
                            >>>>delete
                            >>>>the entries from my delete array.
                            >>>>I don't know whether an operation like this is guaranteed to work in
                            >>>>PHP
                            >>>>- I've never seen it documented, so I suspect not. And just because
                            >>>>it
                            >>>>works in one release under a certain set of conditions is not a
                            >>>>guarantee it will work on another release or under different
                            >>>>condition s.
                            >>>>--
                            >>>>=========== =======
                            >>>>Remove the "x" from my email address
                            >>>>Jerry Stuckle
                            >>>>JDS Computer Training Corp.
                            >>>>jstuck...@a ttglobal.net
                            >>>>=========== =======
                            >>>I never said I disagreed with him -- in fact I, too, generally don't
                            >>>delete elements inside a foreach. However, I will say that when I
                            >>>have done it things seem to work as expected. I guess it all comes
                            >>>down to whether or not the array's internal pointer is modified when
                            >>>you unset the element it's pointing to (I suspect it isn't).
                            >>>I see a lot of questions in these newsgroups that look something like,
                            >>>"What happens if I do X?" or "In PHP is this code valid?" The point I
                            >>>was trying to make (and apparently Jon took offense to it) was that
                            >>>it's easy enough to just try it and see what happens. Software is
                            >>>just that: soft. It can be changed easily enough :)
                            >>Yes, and in a case like this that change can break his code.
                            >>>
                            >>As I said - I've never seen it documented that this is valid. Maybe it
                            >>is and I missed it; I really don't know.
                            >>>
                            >>But this isn't the same as a lot of other "try it and find out"
                            >>questions. In this case it's a known problem in other programming
                            >>languages, and if it is documented that this should or should not work,
                            >>no one has pointed anyone to it.
                            >>>
                            >>And until I see something from the PHP developers saying it is OK, I
                            >>wouldn't do it.
                            >>>
                            >>--
                            >>============= =====
                            >>Remove the "x" from my email address
                            >>Jerry Stuckle
                            >>JDS Computer Training Corp.
                            >>jstuck...@att global.net
                            >>============= =====
                            >This suggests that it's safe (from <http://www.php.net/foreach>):
                            >>
                            >"Unless the array is referenced, foreach operates on a copy of the
                            >specified array and not the array itself. Therefore, the array pointer
                            >is not modified as with the each() construct, and changes to the array
                            >element returned are not reflected in the original array."
                            >>
                            >So unsetting a value in the original array should not affect the copy
                            >that foreach is working on.
                            >
                            >
                            Your right. I saw this after the fact after I did try it out and compared it
                            with a for loop. The foreach worked while the for loop didn't. Then I went
                            to the manual and saw that it worked on a copy.
                            >
                            So in this case it should work just fine. The for looped version actually
                            doesn't work and does give the problem I suspected. (thought I sent a post
                            a bout it though).
                            >
                            So unless they go change how foreach works in a newer version I think it
                            will be safe. I'm still trying to get used to php as it does a lot of things
                            differently than I'm used to.
                            >
                            The issue with "trying it out" is that in things like this you can't be sure
                            since its a implementation artifact... The only way to know is to know that
                            foreach is working on a copy. Of course maybe one could realize that by
                            doing a few examples but when usually one doesn't want to rely on what they
                            think is going on in this sorta situation and needs to know the facts.
                            >
                            Of course now I know its completely safe which was my original question ;)
                            It came from the manual which is where I should have looked in the fist
                            place(I did but I guess I skipped over that part ;/
                            >
                            Thanks,
                            Jon
                            >
                            >
                            Jon,

                            I disagree it's "completely safe". All this is saying is that the
                            changes to the copy will not affect the original. It says nothing about
                            what changes to the original will affect.

                            --
                            =============== ===
                            Remove the "x" from my email address
                            Jerry Stuckle
                            JDS Computer Training Corp.
                            jstucklex@attgl obal.net
                            =============== ===

                            Comment

                            • Jerry Stuckle

                              #15
                              Re: safe to delete elements of array in foreach

                              Schraalhans Keukenmeester wrote:
                              At Fri, 18 May 2007 21:05:36 -0400, Jerry Stuckle let his monkeys type:
                              >
                              >ZeldorBlat wrote:
                              >>On May 18, 11:40 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
                              >>wrote:
                              >>>"ZeldorBla t" <zeldorb...@gma il.comwrote in message
                              >>>>
                              >>>news:1179501 018.125313.6360 0@p77g2000hsh.g ooglegroups.com ...
                              >>>>
                              >>>>On May 18, 11:05 am, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.com>
                              >>>>wrote:
                              >>>>>Is it safe to remove elements from an array that foreach is working on?
                              >>>>>(normall y this is not the case but not sure in php) If so is there an
                              >>>>>efficien t way to handle it? (I could add the indexes to a temp array and
                              >>>>>delete afterwards if necessary but since I'm actually working in a nested
                              >>>>>situatio n this could get a little messy. I guess I could set there values
                              >>>>>to
                              >>>>>null and remove them afterwards?
                              >>>>>Thanks,
                              >>>>>Jon
                              >>>>Why don't you try it and see what happens?
                              >>>Um... cause I did... but that doesn't mean much. Just cause someone tries
                              >>>something doesn't prove that it will always work like that...
                              >>>>
                              >>>got any more bright ideas?
                              >>>>
                              >>>Or is the question to hard for you?
                              >>No, the question is not to (sic) hard for me. But, as you've already
                              >>discovered, it isn't that difficult to test, either.
                              >>>
                              >Sorry, I agree with Jon on this one.
                              >>
                              >I make it a habit not to delete entries in a foreach() loop. Rather, I
                              >build an array of keys I want to delete, and after the loop ends, delete
                              >the entries from my delete array.
                              >>
                              >I don't know whether an operation like this is guaranteed to work in PHP
                              >- I've never seen it documented, so I suspect not. And just because it
                              >works in one release under a certain set of conditions is not a
                              >guarantee it will work on another release or under different conditions.
                              >
                              I agree 100% with you not to rely on undocumented 'features'.
                              Just out of curiosity I took this one step further and discovered the
                              following (again, can't rely on this to hold true unless it's documented
                              somewhere, well hidden):
                              >
                              <?PHP
                              $array=array('j ohn','james','d elilah','mary') ;
                              foreach ($array as $key=>$value) {
                              echo "$key =$value".NEWLIN E;
                              if ($value == 'delilah') {
                              $array[$key]='samson';
                              }
                              elseif ($value=='james ') {
                              unset($array[$key]);
                              }
                              }
                              echo NEWLINE;
                              foreach ($array as $key=>$value) {
                              echo "$key =$value".NEWLIN E;
                              }
                              ?>
                              0 =john
                              1 =james
                              2 =delilah
                              3 =mary
                              >
                              0 =john
                              2 =samson
                              3 =mary
                              >
                              The foreach loop operates on a copy of the array
                              The original array remains in scope
                              Current($array) points to the first element throughout the loop
                              >
                              I'd think setting elements to NULL directly in the array isn't any better,
                              you're still relying on the same 'feature'.
                              >
                              Sh
                              Yes, foreach() works on a copy. But while the manual indicates changes
                              to the copy don't affect the original - they say nothing about when
                              changes to the original will affect the copy.

                              When I want to delete entries, I keep an array of keys to delete. When
                              I'm done with the array itself, I go through the second array and delete
                              all indicated keys.

                              --
                              =============== ===
                              Remove the "x" from my email address
                              Jerry Stuckle
                              JDS Computer Training Corp.
                              jstucklex@attgl obal.net
                              =============== ===

                              Comment

                              Working...