recreating returned variables under the same name
I have a function that returns an array of variables. The variables it returns vary depending on what it needs to return. For example one time it could return array($pet,$color);
and another time it could return array($height,$width,$table);
On the receiving end I want to make these variables 开发者_JAVA百科available. If I knew I was expecting $pet and $color
, I could do something like
list($pet, $color) = myfunction();
but I don't know what the function is going to return each time. So is there a way I could still recreate these variables under the same names when I receive the function output?
Edit: I was hoping to not have to do it by defining an associative array that has the name of the variable saved as a string in addition to the variable itself.
Does the function return an associative array, eg
return array(
'height' => $height,
'width' => $width,
'table' => $table
);
If so, you can then use the extract
function to bring each entry into the current scope's symbol table
I think you need to use associative arrays instead, so entries will have fixed names associated with them:
array('height'=>$height, 'width'=>$width, 'table'=>$table)
精彩评论