Definition and usage
The PHP extract () function imports variables from the array into 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 conflicts when a variable already exists and the element with the same name in the array.
This function returns the number of variables that were set successfully.
Grammar
Extract (Array,extract_rules,prefix)
| Parameters |
Description |
| Array |
Necessary. Specifies the input to use. |
| extract_rules |
Optional. The extract () function checks to see if each key name is a valid variable name, and also checks whether the variable names in the symbol table conflict. The handling of illegal, numeric, and conflicting key names is determined by this parameter. Can be one of the following values: Possible values:
- extr_overwrite-default. If there is a conflict, overwrite the existing variable.
- Extr_skip-If there is a conflict, do not overwrite the existing variable. (ignores elements with the same name in the array)
- extr_prefix_same-If there is a conflict, precede the variable name with the prefix PREFIX. Since PHP 4.0.5, this also includes the processing of the digital index.
- extr_prefix_all-Prefix all variable names with PREFIX (the third argument).
- extr_prefix_invalid-prefix PREFIX is only preceded by an illegal or numeric variable name. This tag is a new addition to PHP 4.0.5.
- extr_if_exists-Overrides the value of a variable with the same name in the current symbol table only if it already exists. None of the others will deal with it. It can be used where a set of valid variables has been defined and then extracted from an array such as $_request to override those variables. This tag is a new addition to PHP 4.2.0.
- extr_prefix_if_exists-A variable name with the prefix appended to it is created only if a variable with the same name already exists in the current symbol table, and none of the others are processed. This tag is a new addition to PHP 4.2.0.
- extr_refs-Extracts the variable as a reference. This strongly demonstrates that the imported variable still references the value of the Var_array parameter. This flag can be used alone or in conjunction with any other flag in Extract_type. This tag is a new addition to PHP 4.3.0.
|
| Prefix |
Optional. Note that prefix is required only if the value of Extract_type is Extr_prefix_same,extr_prefix_all,extr_prefix_invalid or extr_prefix_if_exists. If the result appended with the prefix is not a valid variable name, it will not be imported into the symbol table. An underscore is automatically added between the prefix and the array key name.
|
Example 1
Copy CodeThe code is as follows:
$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:
Copy CodeThe code is as follows:
$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;
PHP Extract () function
Recently, when looking at a cow's code, see a very useful function: Extract (), its main function is to expand the array, the key name as the variable name, the element value is the variable value, you can say that the operation of the array provides another convenient tool, for example, can be easily extracted $_post or $_ Get elements, the content submitted to the form can not be assigned one by one, directly using the following code:
Form.html
Copy CodeThe code is as follows:
In action.php, simply use the extract () function to unpack the $_post global data:
action.php
Copy CodeThe code is as follows:
Extract ($_post);
Equivalent to $username = $_post[' username '];
$password = $_post[' password ');
?>
Isn't it convenient? Oh, here is the PHP manual in the detailed explanation:
Extract
(PHP 4, PHP 5)
extract-importing variables from an array into the current symbol table
Description
int Extract (array $var _array [, int $extract _type [, String $prefix]]
This function is used to import variables from the array into the current symbol table. Accept the associative array var_array as the argument and use the key name as the variable name, and the value as the value of the variable. For each key/value pair, the variables are created in the current symbol table and are affected by the Extract_type and prefix parameters.
Note: From version 4.0.5 This function returns the number of variables that were extracted.
Note:extr_if_exists and Extr_prefix_if_exists are introduced in version 4.2.0.
Note:extr_refs was introduced in version 4.3.0.
Extract () checks each key name to see if it can be used as a valid variable name and also checks for conflicts with variable names already in the symbol table. Methods that treat illegal/numeric and conflicting key names are determined according to the Extract_type parameter. Can be one of the following values:
Extr_overwrite
If there is a conflict, overwrite the existing variable.
Extr_skip
If there is a conflict, do not overwrite the existing variable.
Extr_prefix_same
If there is a conflict, precede the variable name with the prefix prefix.
Extr_prefix_all
Prefix all variable names with prefix. From PHP 4.0.5 This also includes the processing of the digital index.
Extr_prefix_invalid
Prefix prefix only with illegal/numeric variable names. This tag is a new addition to PHP 4.0.5.
Extr_if_exists
Overrides their values only if they already have a variable with the same name in the current symbol table. None of the others will deal with it. It can be used where a set of valid variables has been defined and then extracted from an array such as $_request to override those variables. This tag is a new addition to PHP 4.2.0.
Extr_prefix_if_exists
A variable name with a prefix appended to it is created only if a variable with the same name already exists in the current symbol table, and none of the others are processed. This tag is a new addition to PHP 4.2.0.
Extr_refs
Extracts the variable as a reference. This strongly demonstrates that the imported variable still references the value of the Var_array parameter. This flag can be used alone or in conjunction with any other flag in Extract_type. This tag is a new addition to PHP 4.3.0.
If Extract_type is not specified, it is assumed to be extr_overwrite.
Note prefix is required only if the value of Extract_type is Extr_prefix_same,extr_prefix_all,extr_prefix_invalid or extr_prefix_if_exists. If the result appended with the prefix is not a valid variable name, it will not be imported into the symbol table. An underscore is automatically added between the prefix and the array key name.
Extract () returns the number of variables successfully imported into the symbol table.
Warning
Do not use Extract () for data that cannot be trusted, such as the user's input ($_get, ...). )。 If you do this, for example, to temporarily run old code that relies on register_globals, be sure to use extract_type values that will not be overwritten, such as Extr_skip, and be aware that you should follow the Variables_order in php.ini Define the order to extract.
One possible use of extract () is to import the contents of the associative array returned by Wddx_deserialize () into the symbol table variable.
Example#1 Extract () example
Copy CodeThe code is as follows:
/* Assume that $var _array is an array returned by Wddx_deserialize */
$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";
?>
The example above will output:
Blue, large, sphere, medium
The $size is not overwritten because Extr_prefix_same is specified, which makes $WDDX _size be built. If Extr_skip is specified, the $WDDX _size will not be established. Extr_overwrite will make the value of the $size "medium", Extr_prefix_all will create a new variable $wddx _color, $wddx _size and $wddx _shape.
You must use associative arrays, and arrays of numeric indexes will not produce results unless you use Extr_prefix_all or Extr_prefix_invalid.
http://www.bkjia.com/PHPjc/325734.html www.bkjia.com true http://www.bkjia.com/PHPjc/325734.html techarticle define and use the PHP extract () function to import variables from the array into 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. Second parameter ...