...

View Full Version : Getting Variables from Arrays from Functions



Yaggles
07-01-2005, 06:51 AM
I have the following 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

delinear
07-01-2005, 12:01 PM
Well... based on your code example, you could do it like this:

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:

$variable = array('var1' => 'val1');
Except the function wrapper will make the first example slower than the second.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum