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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jan 2005
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Getting Variables from Arrays from Functions

    I have the following code:
    PHP Code:
    <?
    function assign_vars($vars = array())
    {
        foreach(
    $vars AS $var => $val)
        {
            echo 
    $var ' => ' $val '<BR />';
        }
    }
    $variable assign_vars(array('var1' => 'val1'));
    ?>
    I am trying to be able to get $variable['var1'] to equal 'val1'. How could I do this?

    Thanks,
    -Yaggles

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well... based on your code example, you could do it like this:
    PHP Code:
    function assign_vars($vars) {
        return 
    $vars;    

    $variable assign_vars(array('var1' => 'val1'));
    print_r($variable);

    // results in:

    //  Array
    //  (
    //      [var1] => val1
    //  ) 
    It's essentially exactly the same as doing:
    PHP Code:
    $variable = array('var1' => 'val1'); 
    Except the function wrapper will make the first example slower than the second.


  •  

    Posting Permissions

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