extract()
extract() — Import variables into the current symbol table from an array.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
Signature
extract(array &$array, int $flags = EXTR_OVERWRITE, string $prefix = ""): int
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| &$array | array | An associative array. This function treats keys as variable names and values as variable values. For each key/value pair it will create a variable in the current symbol table, subject to flags and prefix parameters. |
| $flags | int | The way invalid/numeric keys and collisions are treated is determined by the extraction flags. It can be one of the following values: EXTR_OVERWRITE If there is a collision, overwrite the existing variable. EXTR_SKIP If there is a collision, don't overwrite the existing variable. EXTR_PREFIX_SAME If there is a collision, prefix the variable name with prefix. EXTR_PREFIX_ALL Prefix all variable names with prefix. ... |
| $prefix | string | Note that prefix is only required if flags is EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS. If the prefixed result is not a valid variable name, it is not imported into the symbol table. Prefixes are automatically separated from the array key by an underscore character. |
Return value
Returns the number of variables successfully imported into the symbol table.
Example
A runnable snippet:
$size = "large"; $var_array = array( "color" => "blue", "size" => "medium", "shape" => "sphere" ); extract($var_array, EXTR_PREFIX_SAME, "wddx"); echo "$color, $size, $shape, $wddx_size\n";
Output:
blue, large, sphere, medium
Changelog
| Version | Change |
|---|---|
| 8.0.0 | Now throws a ValueError for an invalid flags or prefix. Previously, an E_WARNING was raised and null was returned. |
See also
All PHP 8.5 built-in functions
Reference card built from the PHP manual page extract() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.