Results 1 to 4 of 4

Thread: Learning PHP: Trouble with function that alters array

  1. #1
    Join Date
    Aug 2005
    Location
    Michigan
    Beans
    56

    Learning PHP: Trouble with function that alters array

    Hi,

    I've been spending the last few days trying to learn PHP, and one of the examples I've come up with to try and use arrays has got me scratching my head.

    Basically, I've created a function remove_lake() which takes two arguments: the name of a lake, and the name of the array hopefully containing the lake. It then searches the array, and if the lake is found, it removes it from the array. If not found, it tells you that lake wasn't in the array.

    What I'm noticing is that when calling my function and giving it the variables to be used, no change occurs.

    I'm thinking it may be a scope issue, but being new to PHP, I'm not sure yet how to get around it.

    Here is my code (please note: the assignment in remove_lake() is on purpose, and is not a botched attempt at comparison)
    PHP Code:
    <?php

    // Setup base array with lakes
    $locations = array('Sheila Lake''Houghton Lake''Four Mile Lake',
                       
    'Leeke Lake''Green Lake');

    // Define some functions to operate on the array
    function show_lakes($in_data) {
        
    $lakes count($in_data);
        print 
    "There are $lakes lakes stored in the array:";
        print 
    "<br />";

        foreach(
    $in_data as $place) {
        print 
    "$place <br />";
        }
    }

    function 
    remove_lake($lakename$lakearray) {
        
    // the following assignment is deliberate
        
    if($key array_search($lakename$lakearray)) {
            unset(
    $lakearray[$key]);
        }
        else print 
    "<br />## That lake was not found. ## <br /><br />";
    }

    // test the functions
    print " --showing default array -- <br />";
    show_lakes($locations);
    print 
    "<br /><br />";

    // add a new lake
    $locations[] = 'Joslin Lake';

    print 
    " --showing additional entry to array -- <br />";
    show_lakes($locations);
    print 
    "<br /><br />";

    // remove a previous lake
    print " --showing removal of entry from array -- <br />";
    remove_lake('Green Lake'$locations);
    show_lakes($locations);
    print 
    "<br /><br />";


    ?>
    Thanks all; any guidance or hints are much appreciated!
    "Many go fishing all their lives without knowing that it is not fish they are after."
    - Henry David Thoreau

  2. #2
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

    Re: Learning PHP: Trouble with function that alters array

    It is indeed a scope issue. Normally, when you pass the $lakearray argument to your remove_lake() function, a copy of this object is created in memory, and the function will only be able to manipulate that copy, not the original (here, the $locations object in your main program).

    You have three ways to achieve the desired goal:

    -> Make the $locations variable accessible from inside your function using the GLOBAL keyword. This is usually considered bad practice and you should avoid it, but here's an example for information:

    Code:
    firas@nobue ~ % cat test.php
    <?php
    $locations = array("foo", "bar");
    
    function remove_lake($lakename)
    {
            GLOBAL $locations;
            // the following assignment is deliberate
            if(($key = array_search($lakename, $locations)) !== false) {
                    unset($locations[$key]);
            }
            else print "<br />## That lake was not found. ## <br /><br />";
    }
    
    remove_lake("foo");
    var_dump($locations);
    ?>
    firas@nobue ~ % php test.php
    array(1) {
      [1]=>
      string(3) "bar"
    }
    -> Use the return value of the function to pass the modified array to the rest of your program:

    Code:
    firas@nobue ~ % cat test.php
    <?php
    $locations = array("foo", "bar");
    
    function remove_lake($lakename, $lakearray)
    {
            // the following assignment is deliberate
            if(($key = array_search($lakename, $lakearray)) !== false) {
                    unset($lakearray[$key]);
                    return $lakearray;
            }
            else {
                    print "<br />## That lake was not found. ## <br /><br />";
                    return NULL;
            }
    }
    
    $locations = remove_lake("foo", $locations);
    var_dump($locations);
    ?>
    firas@nobue ~ % php test.php
    array(1) {
      [1]=>
      string(3) "bar"
    }
    -> Use object-oriented programming, but that will take a bit more work. You can stick with #2 for now.


    Also, notice that I changed the test in your function. Try to find out by yourself why yours was wrong.
    Last edited by Bachstelze; January 6th, 2009 at 09:13 PM.
    「明後日の夕方には帰ってるからね。」


  3. #3
    Join Date
    Aug 2005
    Location
    Michigan
    Beans
    56

    Re: Learning PHP: Trouble with function that alters array

    Thanks for such a fast and informative reply!

    I'm guessing my test is wrong because nothing was actually being compared; I was thinking that just assigning $key a value would make it operate as if I just used if(non-zero value) so that it would evaluate as true.

    But now I realize that if the lake being searched for was the first in the array, it would assign $key = 0, which would make the statement false and prevent unset() from ever occurring.

    That may not be all that was wrong with it (don't tell me yet ), so I'm definitely going to be giving that some thinking on my way home.

    Thanks again for the help
    "Many go fishing all their lives without knowing that it is not fish they are after."
    - Henry David Thoreau

  4. #4
    Join Date
    Jul 2008
    Beans
    1,491

    Re: Learning PHP: Trouble with function that alters array

    I'm surprised nobody has yet mentioned that PHP is aware of pass-by-reference (aka pointers):
    PHP Code:
    <?php

    $array
    [0]="foo";

    function 
    bar($array) {
        
    $array[0]="bar";
    }

    bar(&$array);

    die(
    var_dump($array));

    ?>
    Three guesses what this outputs?

Bookmarks

Posting Permissions

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