The phpextract () function converts data variables to php variables. The PHPextract () 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 import the variable from the array to the current symbol table by the PHP extract () function. 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.
Export the variables from the PHP array and register them as global variables. The key name is used as the variable name and the value is used as the variable value, as shown below:
The code is as follows: |
|
$ Vars = array ('var1' => '1', 'var2' => '2', 'var3' => '3 ', 'var4' => '4', 'var5' => '5 '); |
Implement access by using the key name as the variable name, such as: $ var1, $ var2
Solution 1: Use the PHP built-in extract () function. the method is as follows:
The code is as follows: |
|
Extract ($ vars ); |
Solution 2: Use the foreach loop array to register as a global variable. the method is as follows:
The code is as follows: |
|
Foreach ($ vars as $ k =>$ v ){ $ GLOBALS [$ k] = $ v; } |
The second solution is recommended because the extract () function has performance and security problems.
Extract () function description
(PHP 3> = 3.0.7, PHP 4, PHP 5)
Extract -- import the variable from the array to the current symbol table
The code is as follows: |
|
Int extract (array var_array [, int extract_type [, string prefix]) |
This function is used to import variables from the array to the current symbol table. The array var_array is used as the parameter and the key name is used as the variable name, and the value is used as the variable value. Each key/value pair will create a variable in the current symbol table and be affected by the extract_type and prefix parameters.
Reference Table
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. |
Use all parameters: