PHP » PYTHON |
login |
register |
about
|
|
From Python equivalent of PHP’s compact() and extract() on Stack Overflow.
def extract(vars): caller = inspect.stack()[1][0] # caller of extract() for n, v in vars.items(): caller.f_locals[n] = v # NEVER DO THIS ;-) extract(PHP 4, PHP 5) extract — Import variables into the current symbol table from an array Description
int extract
( array $var_array
[, int $extract_type= EXTR_OVERWRITE
[, string $prefix
]] )
Import variables from an array into the current symbol table. extract() checks each key to see whether it has a valid variable name. It also checks for collisions with existing variables in the symbol table. Parameters
Return ValuesReturns the number of variables successfully imported into the symbol table. Changelog
Examples
Example #1 extract() example A possible use for extract() is to import into the symbol table variables contained in an associative array returned by wddx_deserialize().
<?phpThe above example will output: blue, large, sphere, medium The $size wasn't overwritten, because we specified EXTR_PREFIX_SAME, which resulted in $wddx_size being created. If EXTR_SKIP was specified, then $wddx_size wouldn't even have been created. EXTR_OVERWRITE would have caused $size to have value "medium", and EXTR_PREFIX_ALL would result in new variables being named $wddx_color, $wddx_size, and $wddx_shape.
NotesWarning
Do not use extract() on untrusted data, like user-input ($_GET, ...). If you do, for example, if you want to run old code that relies on register_globals temporarily, make sure you use one of the non-overwriting extract_type values such as EXTR_SKIP and be aware that you should extract in the same order that's defined in variables_order within the php.ini. |
more
Recently updated
more
Most requested
|