reference passing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Noel Wood

    reference passing

    Hello, I have a function I use to remove all the whitespace from the
    beginning and end of all the $_REQUEST variables. A $_REQUEST variable may
    also be an array so I call it recursively. I have used as a formal
    parameter a referenced passed variable. So any changes to the array inside
    the function should also occur to the array outside the function.
    The function I use is shown below...

    //the array pointer must be reset to the start before this function is
    called
    // for the first time. It would be a safe idea to call
    resset($inputVa riable) after last use as well.
    function removeWhitespac eRecursively(&$ anArray){
    while (list($key,$val ) = each ($anArray)){
    if (is_array($val) ){
    removeWhitespac eRecursively($v al);
    $anArray[$key] = $val; //unknown why required.
    }else{
    $anArray[$key] = trim($val);

    }
    }
    }
    In testing this function. It worked well for simple $_REQUEST variables
    that had no arrays in them but when an array was in the $_REQUEST array then
    the line marked "//unknown why required" was found to be needed otherwise
    the changes to the recursively altered array did not propagate to the
    original array.
    eg...
    given the input...
    $_REQUEST['name'] = " noel ";
    $_REQUEST['phone'][0] = " 1234 ";
    $_REQUEST['phone'][1] = " 5678 ";

    The output without the marked line was...
    $_REQUEST['name'] = "noel";
    $_REQUEST['phone'][0] = " 1234 ";
    $_REQUEST['phone'][1] = " 5678 ";

    The output with the marked line was...
    $_REQUEST['name'] = "noel";
    $_REQUEST['phone'][0] = "1234";
    $_REQUEST['phone'][1] = "5678";

    Can anyone please explain why the changes to the recursively changed array
    did not propagate to the original array?
    Thanks for your help.


  • Mike Duganets

    #2
    Re: reference passing



    Noel Wood wrote:
    [color=blue]
    > Hello, I have a function I use to remove all the whitespace from the
    > beginning and end of all the $_REQUEST variables. A $_REQUEST variable may
    > also be an array so I call it recursively. I have used as a formal
    > parameter a referenced passed variable. So any changes to the array inside
    > the function should also occur to the array outside the function.
    > The function I use is shown below...
    >
    > //the array pointer must be reset to the start before this function is
    > called
    > // for the first time. It would be a safe idea to call
    > resset($inputVa riable) after last use as well.
    > function removeWhitespac eRecursively(&$ anArray){
    > while (list($key,$val ) = each ($anArray)){
    > if (is_array($val) ){
    > removeWhitespac eRecursively($v al);
    > $anArray[$key] = $val; //unknown why required.[/color]

    $val - is a copy of array $anArray[$key]
    you pass copy to your function, modify it
    and after thar you _must_ assign it to your
    target output array $anArray[$key]
    [color=blue]
    > }else{
    > $anArray[$key] = trim($val);
    >
    > }
    > }
    > }
    > In testing this function. It worked well for simple $_REQUEST variables
    > that had no arrays in them but when an array was in the $_REQUEST array then
    > the line marked "//unknown why required" was found to be needed otherwise
    > the changes to the recursively altered array did not propagate to the
    > original array.
    > eg...
    > given the input...
    > $_REQUEST['name'] = " noel ";
    > $_REQUEST['phone'][0] = " 1234 ";
    > $_REQUEST['phone'][1] = " 5678 ";
    >
    > The output without the marked line was...
    > $_REQUEST['name'] = "noel";
    > $_REQUEST['phone'][0] = " 1234 ";
    > $_REQUEST['phone'][1] = " 5678 ";
    >
    > The output with the marked line was...
    > $_REQUEST['name'] = "noel";
    > $_REQUEST['phone'][0] = "1234";
    > $_REQUEST['phone'][1] = "5678";
    >
    > Can anyone please explain why the changes to the recursively changed array
    > did not propagate to the original array?
    > Thanks for your help.
    >
    >[/color]

    Comment

    • Jasper Bryant-Greene

      #3
      Re: reference passing

      Noel Wood wrote:
      [color=blue]
      > function removeWhitespac eRecursively(&$ anArray){
      > while (list($key,$val ) = each ($anArray)){
      > if (is_array($val) ){
      > removeWhitespac eRecursively($v al);
      > $anArray[$key] = $val; //unknown why required.
      > }else{
      > $anArray[$key] = trim($val);
      >
      > }
      > }
      > }
      >
      > Can anyone please explain why the changes to the recursively changed array
      > did not propagate to the original array?
      > Thanks for your help.[/color]

      Because when you get list($key,$val) out of $anArray you are making a
      copy of the variable, not a reference to the variable. You can work
      around this by passing $anArray[$key] to removeWhitespac eRecursively()
      when calling it from within the function.

      A better way to implement this function would be:

      function removeWhitespac eRecursively(&$ anArray) {
      foreach($anArra y as $key=>$val) {
      if(is_array($va l))
      removeWhitespac eRecursively($a nArray[$key]);
      else
      $anArray[$key] = trim($val);
      }
      }

      --
      Jasper Bryant-Greene
      Cabbage Promotions

      Comment

      Working...