Document directory
Definition and usage
The PHP extract () function imports variables from the array to the current symbol table.
For each element in the array, the key name is used for the variable name, and the key value is used for the variable value.
The second parameter type is used to specify how the extract () function treats such conflicts when a variable already exists and an element with the same name exists in the array.
This function returns the number of successfully set variables.
Syntax
extract(array,extract_rules,prefix)
| Parameters |
Description |
| Array |
Required. Specifies the input to be used. |
| Extract_rules |
Optional. The extract () function checks whether each key name is a valid variable name and whether it is in conflict with the variable name in the symbol table.
The processing of illegal, numbers, and conflicting key names is determined by this parameter. It can be one of the following values:
Possible values:
- EXTR_OVERWRITE-default. If a conflict exists, the existing variables are overwritten.
- EXTR_SKIP-if there is a conflict, the existing variables are not overwritten. (Ignore elements with the same name in the array)
- EXTR_PREFIX_SAME-if there is a conflict, add the prefix before the variable name. Since PHP 4.0.5, this also includes processing digital indexes.
- EXTR_PREFIX_ALL-prefix all variable names (the third parameter ).
- EXTR_PREFIX_INVALID-only prefix before invalid or numeric variable names. This mark is newly added to PHP 4.0.5.
- EXTR_IF_EXISTS-only overwrite the values of variables with the same name in the current symbol table. None of them are processed. It can be used for variables that have defined a combination, and then extract values from an array such as $ _ REQUEST to overwrite these variables. This mark is newly added to PHP 4.2.0.
- EXTR_PREFIX_IF_EXISTS. This mark is newly added to PHP 4.2.0.
- EXTR_REFS-extract variables as references. This effectively demonstrates that the imported variable still references the value of the var_array parameter. This flag can be used independently OR in extract_type OR with any other flag. This mark is newly added to PHP 4.3.0.
|
| Prefix |
Optional. Note that prefix is only required when the value of extract_type is EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS. If the result with a prefix is not a valid variable name, it is not imported to the symbol table.
An underline is automatically added between the prefix and the array key name. |
Example 1
<?php
$a = 'Original';
$my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse");
extract($my_array);
echo "\$a = $a; \$b = $b; \$c = $c";
?>
Output:
$a = Cat; $b = Dog; $c = Horse
Example 2
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;";
?>
Output:
$a = Original; $b = Dog; $c = Horse; $dup_a = Cat;