PHP extract()Functions
Examples
Assign the values "Cat", "Dog", and "Horse" to the variables $a, $b, and $c:
$a = "Original";
$my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse");
extract($my_array);
echo "\$a = $a; \$b = $b; \$c = $c";
Run example »
Definition and Usage
The extract() function imports variables from an array into the current symbol table.
This function uses array key names as variable names and array key values as variable values. For each element in the array, a corresponding variable is created in the current symbol table.
This function returns the number of variables successfully set.
Syntax
extract(array,extract_rules,prefix)
| Parameters | Description |
|---|---|
| array | Required. Specifies the array to use. |
| extract_rules | Optional. The extract() function checks whether each key name is a valid variable name and also whether it conflicts with existing variable names in the symbol table. The handling of invalid and conflicting key names is determined by this parameter. Possible values:
|
| prefix | Optional. If the value of the extract_rules parameter is EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID, or EXTR_PREFIX_IF_EXISTS, then prefix is required. This parameter specifies the prefix. An underscore is automatically added between the prefix and the array key name. |
Technical Details
| Return value: | Returns the number of variables successfully set. |
|---|---|
| PHP Version: | 4+ |
| Changelog: | extract_rulesThe value EXTR_REFS was added in PHP 4.3. extract_rulesThe values EXTR_IF_EXISTS and EXTR_PREFIX_IF_EXISTS were added in PHP 4.2. Since PHP 4.0.5, this function returns the number of variables successfully set. extract_rulesThe value EXTR_PREFIX_INVALID was added in PHP 4.0.5. Since PHP 4.0.5,extract_rulesThe value EXTR_PREFIX_ALL also includes numeric variables. |
More Examples
Example 1
Use all parameters:
<?php
$a = "Original";
$my_array = array("a" => "Cat", "b" => "Dog", "c" => "Horse");
extract($my_array, EXTR_PREFIX_SAME, "dup");
echo "$a = $a; $b = $b; $c = $c; $dup_a = $dup_a";
?>
$a = "Original";
$my_array = array("a" => "Cat", "b" => "Dog", "c" => "Horse");
extract($my_array, EXTR_PREFIX_SAME, "dup");
echo "$a = $a; $b = $b; $c = $c; $dup_a = $dup_a";
?>
Run example »
Complete PHP Array Reference Other Extensions