Weird $_POST behavior when trying to unset elements

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • comp.lang.php

    Weird $_POST behavior when trying to unset elements

    [PHP]
    // NEW 11/27/2006: FINALLY, IF YOU ADDED OR DELETED OR DID ANY KIND OF
    FORM ACTION SUCCESSFULLY, DON'T RE-DISPLAY THE NEW EXPENSE ITEMS VIA
    $_POST
    if ($_POST['hasUpdatedLeas e'] && (!is_array($lea seObj->errorArray) ||
    @sizeof($leaseO bj->errorArray) == 0)) {
    print_r(array_k eys($_POST));
    @reset($_POST);
    $tempPost = $_POST;
    foreach ($_POST as $key =$val) if (strpos($key, 'new_') === 0)
    array_remove($t empPost, $tempPost[$key]);
    @reset($_POST);
    $_POST =& $tempPost;
    print_r(array_k eys($_POST));
    }
    [/PHP]

    For some reason, even though the condition is always met when I click
    any of the form submit images, the following "weird behavior patterns"
    occur:

    1) If I click the "Add" submit image, all $_POST values whose keys
    start with "new_" should be removed from $_POST. However,
    $_POST['new_audited'] and $_POST['new_reoccuring '] remain

    2) If I click the "Delete" submit image, all $_POST values whose keys
    start with "new_" should be removed from $_POST. However, *ALL* of
    them remain in $_POST untouched!

    I can verify that I enter the conditional block by my arrays appearing
    on screen every time.

    Any reason why this is happening that I missed?

    Thanx
    Phil

  • Kimmo Laine

    #2
    Re: Weird $_POST behavior when trying to unset elements

    "comp.lang. php" <phillip.s.powe ll@gmail.comwro te in message
    news:1164699920 .068844.217930@ j72g2000cwa.goo glegroups.com.. .
    [PHP]
    // NEW 11/27/2006: FINALLY, IF YOU ADDED OR DELETED OR DID ANY KIND OF
    FORM ACTION SUCCESSFULLY, DON'T RE-DISPLAY THE NEW EXPENSE ITEMS VIA
    $_POST
    if ($_POST['hasUpdatedLeas e'] && (!is_array($lea seObj->errorArray) ||
    @sizeof($leaseO bj->errorArray) == 0)) {
    print_r(array_k eys($_POST));
    @reset($_POST);
    $tempPost = $_POST;
    foreach ($_POST as $key =$val) if (strpos($key, 'new_') === 0)
    array_remove($t empPost, $tempPost[$key]);
    @reset($_POST);
    $_POST =& $tempPost;
    print_r(array_k eys($_POST));
    }
    [/PHP]
    >
    For some reason, even though the condition is always met when I click
    any of the form submit images, the following "weird behavior patterns"
    occur:
    >
    1) If I click the "Add" submit image, all $_POST values whose keys
    start with "new_" should be removed from $_POST. However,
    $_POST['new_audited'] and $_POST['new_reoccuring '] remain
    >
    2) If I click the "Delete" submit image, all $_POST values whose keys
    start with "new_" should be removed from $_POST. However, *ALL* of
    them remain in $_POST untouched!
    >
    I can verify that I enter the conditional block by my arrays appearing
    on screen every time.
    >
    Any reason why this is happening that I missed?


    Never heard of array_remove, I wonder what it does. Could it be it's not
    actually removing anything? Why not just:
    unset($tempPost[$key]);

    --
    "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
    http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
    spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)


    Comment

    • comp.lang.php

      #3
      Re: Weird $_POST behavior when trying to unset elements


      Kimmo Laine wrote:
      "comp.lang. php" <phillip.s.powe ll@gmail.comwro te in message
      news:1164699920 .068844.217930@ j72g2000cwa.goo glegroups.com.. .
      [PHP]
      // NEW 11/27/2006: FINALLY, IF YOU ADDED OR DELETED OR DID ANY KIND OF
      FORM ACTION SUCCESSFULLY, DON'T RE-DISPLAY THE NEW EXPENSE ITEMS VIA
      $_POST
      if ($_POST['hasUpdatedLeas e'] && (!is_array($lea seObj->errorArray) ||
      @sizeof($leaseO bj->errorArray) == 0)) {
      print_r(array_k eys($_POST));
      @reset($_POST);
      $tempPost = $_POST;
      foreach ($_POST as $key =$val) if (strpos($key, 'new_') === 0)
      array_remove($t empPost, $tempPost[$key]);
      @reset($_POST);
      $_POST =& $tempPost;
      print_r(array_k eys($_POST));
      }
      [/PHP]

      For some reason, even though the condition is always met when I click
      any of the form submit images, the following "weird behavior patterns"
      occur:

      1) If I click the "Add" submit image, all $_POST values whose keys
      start with "new_" should be removed from $_POST. However,
      $_POST['new_audited'] and $_POST['new_reoccuring '] remain

      2) If I click the "Delete" submit image, all $_POST values whose keys
      start with "new_" should be removed from $_POST. However, *ALL* of
      them remain in $_POST untouched!

      I can verify that I enter the conditional block by my arrays appearing
      on screen every time.

      Any reason why this is happening that I missed?
      >
      >
      >
      Never heard of array_remove, I wonder what it does. Could it be it's not
      actually removing anything? Why not just:
      unset($tempPost[$key]);
      oh sorry I forgot to include the function:

      if (!function_exis ts('array_remov e')) { // FUTURISTIC: IN CASE AN
      "array_remo ve" PHP FUNCTION IS MADE PART OF CORE IN THE FUTURE
      /**
      * Remove a specific element from the array. If not found return the
      array as-is
      *
      * @access public
      * @param array $array (reference)
      * @param mixed $element
      */
      function array_remove(&$ array, $element) {
      if ($element && @in_array($elem ent, $array))
      unset($array[array_search($e lement, $array)]);
      }
      }
      >
      --
      "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
      http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
      spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)

      Comment

      • Kimmo Laine

        #4
        Re: Weird $_POST behavior when trying to unset elements

        "comp.lang. php" <phillip.s.powe ll@gmail.comwro te in message
        news:1164702698 .875321.253830@ l12g2000cwl.goo glegroups.com.. .
        Kimmo Laine wrote:

        Never heard of array_remove, I wonder what it does. Could it be it's not
        actually removing anything? Why not just:
        unset($tempPost[$key]);
        >
        oh sorry I forgot to include the function:
        >
        if (!function_exis ts('array_remov e')) { // FUTURISTIC: IN CASE AN
        "array_remo ve" PHP FUNCTION IS MADE PART OF CORE IN THE FUTURE
        /**
        * Remove a specific element from the array. If not found return the
        array as-is
        *
        * @access public
        * @param array $array (reference)
        * @param mixed $element
        */
        function array_remove(&$ array, $element) {
        if ($element && @in_array($elem ent, $array))
        unset($array[array_search($e lement, $array)]);
        }
        }

        Still, why not just unset the element right away. Now you're risking of
        removing the wrong element when you remove something by the value, not key.
        Imagine a case like this:
        $foo = array('0','0',' 0','0','0');

        If you tell me to remove the element that has the value '0', how am I gonna
        know which element it is, if there are five zeros? Instead, if you tell me
        to remove the fourth element, I have no trouble telling which element you
        mean. I think the real problem here is actually that wrong elements are
        removed from the array. because they ahve the same value. It says in the
        manual: "If $needle is found in $haystack more than once, the first matching
        key is returned."

        Just try this code and see what happens:
        $foo = array('0','0',' 0','0','0');
        print_r($foo);
        array_remove($f oo, $foo[4]);
        print_r($foo); // Was the fourth element removed? I think not.

        And then try this:
        $foo = array('0','0',' 0','0','0');
        print_r($foo);
        unset($foo[4]);
        print_r($foo); // *Now* was the fourth element removed?


        --
        "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
        http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
        spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)


        Comment

        • comp.lang.php

          #5
          Re: Weird $_POST behavior when trying to unset elements


          Kimmo Laine wrote:
          "comp.lang. php" <phillip.s.powe ll@gmail.comwro te in message
          news:1164702698 .875321.253830@ l12g2000cwl.goo glegroups.com.. .
          Kimmo Laine wrote:
          >
          Never heard of array_remove, I wonder what it does. Could it be it's not
          actually removing anything? Why not just:
          unset($tempPost[$key]);
          oh sorry I forgot to include the function:

          if (!function_exis ts('array_remov e')) { // FUTURISTIC: IN CASE AN
          "array_remo ve" PHP FUNCTION IS MADE PART OF CORE IN THE FUTURE
          /**
          * Remove a specific element from the array. If not found return the
          array as-is
          *
          * @access public
          * @param array $array (reference)
          * @param mixed $element
          */
          function array_remove(&$ array, $element) {
          if ($element && @in_array($elem ent, $array))
          unset($array[array_search($e lement, $array)]);
          }
          }
          >
          >
          Still, why not just unset the element right away. Now you're risking of
          removing the wrong element when you remove something by the value, not key.
          Imagine a case like this:
          $foo = array('0','0',' 0','0','0');

          Then I would use array_remove_el ement_at() instead:

          if (!function_exis ts('array_remov e_element_at')) { // FUTURISTIC: MODEL
          AFTER JAVA Vector.removeEl ementAt(index)
          /**
          * Function modeled after {@link
          http://java.sun.com/j2se/1.4.2/docs/...eElementAt(int)
          java.util.Vecto r.removeElement At((integer)ind ex)}
          *
          * Unlike Java, in PHP you will remove element at array index and
          return it
          *
          * @access public
          * @param array $array (reference)
          * @param mixed $key
          * @return mixed $element
          */
          function array_remove_el ement_at(&$arra y, $key = '') {
          if (is_numeric($ke y) || (!is_numeric($k ey) && $array[$key])) {
          if (is_numeric($ke y)) {
          $element = $array[$key];
          unset($array[$key]);
          $array = @array_values($ array); // RE-ORDER ENUMERATIVE ARRAY
          } elseif (!is_numeric($k ey) && $array[$key]) {
          $element = $array[$key];
          unset($array[$key]);
          }
          }
          return $element;
          }
          }

          Phil
          >
          If you tell me to remove the element that has the value '0', how am I gonna
          know which element it is, if there are five zeros? Instead, if you tell me
          to remove the fourth element, I have no trouble telling which element you
          mean. I think the real problem here is actually that wrong elements are
          removed from the array. because they ahve the same value. It says in the
          manual: "If $needle is found in $haystack more than once, the first matching
          key is returned."
          >
          Just try this code and see what happens:
          $foo = array('0','0',' 0','0','0');
          print_r($foo);
          array_remove($f oo, $foo[4]);
          print_r($foo); // Was the fourth element removed? I think not.
          >
          And then try this:
          $foo = array('0','0',' 0','0','0');
          print_r($foo);
          unset($foo[4]);
          print_r($foo); // *Now* was the fourth element removed?
          >
          >
          --
          "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
          http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
          spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)

          Comment

          • comp.lang.php

            #6
            Re: Weird $_POST behavior when trying to unset elements


            comp.lang.php wrote:
            Kimmo Laine wrote:
            "comp.lang. php" <phillip.s.powe ll@gmail.comwro te in message
            news:1164702698 .875321.253830@ l12g2000cwl.goo glegroups.com.. .
            Kimmo Laine wrote:

            Never heard of array_remove, I wonder what it does. Could it be it's not
            actually removing anything? Why not just:
            unset($tempPost[$key]);
            >
            oh sorry I forgot to include the function:
            >
            if (!function_exis ts('array_remov e')) { // FUTURISTIC: IN CASE AN
            "array_remo ve" PHP FUNCTION IS MADE PART OF CORE IN THE FUTURE
            /**
            * Remove a specific element from the array. If not found return the
            array as-is
            *
            * @access public
            * @param array $array (reference)
            * @param mixed $element
            */
            function array_remove(&$ array, $element) {
            if ($element && @in_array($elem ent, $array))
            unset($array[array_search($e lement, $array)]);
            }
            }

            Still, why not just unset the element right away. Now you're risking of
            removing the wrong element when you remove something by the value, not key.
            Imagine a case like this:
            $foo = array('0','0',' 0','0','0');
            >
            >
            Then I would use array_remove_el ement_at() instead:
            >
            if (!function_exis ts('array_remov e_element_at')) { // FUTURISTIC: MODEL
            AFTER JAVA Vector.removeEl ementAt(index)
            /**
            * Function modeled after {@link
            http://java.sun.com/j2se/1.4.2/docs/...eElementAt(int)
            java.util.Vecto r.removeElement At((integer)ind ex)}
            *
            * Unlike Java, in PHP you will remove element at array index and
            return it
            *
            * @access public
            * @param array $array (reference)
            * @param mixed $key
            * @return mixed $element
            */
            function array_remove_el ement_at(&$arra y, $key = '') {
            if (is_numeric($ke y) || (!is_numeric($k ey) && $array[$key])) {
            if (is_numeric($ke y)) {
            $element = $array[$key];
            unset($array[$key]);
            $array = @array_values($ array); // RE-ORDER ENUMERATIVE ARRAY
            } elseif (!is_numeric($k ey) && $array[$key]) {
            $element = $array[$key];
            unset($array[$key]);
            }
            }
            return $element;
            }
            }
            >
            Phil

            If you tell me to remove the element that has the value '0', how am I gonna
            know which element it is, if there are five zeros? Instead, if you tellme
            to remove the fourth element, I have no trouble telling which element you
            mean. I think the real problem here is actually that wrong elements are
            removed from the array. because they ahve the same value. It says in the
            manual: "If $needle is found in $haystack more than once, the first matching
            key is returned."

            Just try this code and see what happens:
            $foo = array('0','0',' 0','0','0');
            print_r($foo);
            array_remove($f oo, $foo[4]);
            print_r($foo); // Was the fourth element removed? I think not.

            And then try this:
            $foo = array('0','0',' 0','0','0');
            print_r($foo);
            unset($foo[4]);
            print_r($foo); // *Now* was the fourth element removed?
            FIXED!

            Got it, apparently my own blunder; I was using the wrong user-defined
            function: instead of array_remove(), I needed
            array_remove_el ement_at():

            if (!function_exis ts('array_remov e_element_at')) { // FUTURISTIC: MODEL
            AFTER JAVA Vector.removeEl ementAt(index)
            /**
            * Function modeled after {@link
            http://java.sun.com/j2se/1.4.2/docs/...eElementAt(int)
            java.util.Vecto r.removeElement At((integer)ind ex)}
            *
            * Unlike Java, in PHP you will remove element at array index and
            return it
            *
            * @access public
            * @param array $array (reference)
            * @param mixed $key
            * @return mixed $element
            */
            function array_remove_el ement_at(&$arra y, $key = '') {
            if (is_numeric($ke y) || (!is_numeric($k ey) && $array[$key])) {
            if (is_numeric($ke y)) {
            $element = $array[$key];
            unset($array[$key]);
            $array = @array_values($ array); // RE-ORDER ENUMERATIVE ARRAY
            } elseif (!is_numeric($k ey) && $array[$key]) {
            $element = $array[$key];
            unset($array[$key]);
            }
            }
            return $element;
            }
            }


            $tempPost = $_POST;
            foreach ($_POST as $key =$val) if (strpos(trim($k ey), 'new_') ===0)
            @array_remove_e lement_at($temp Post, $key);
            $_POST =& $tempPost;

            Also obviously don't need to use reset() either since the array pointer
            is irrelevant in this case, at least I think so..

            Phil

            --
            "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
            http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
            spam@outolempi. net | rot13(xvzzb@bhg byrzcv.arg)

            Comment

            Working...