Use PHP extract function

PHP extract()

description

Function of the variable from the array into the current symbol table.
For each element in the array, key variable names, variable values for the key.
The second parameter type is used to specify when a variable already exists, while the array elements have the same name, extract () function is how to deal with such conflicts.

return value

This function returns the number of variables successfully set.

usage

extract(array,extract_rules,prefix)
parameter description
array essential. Provides for the use of input
extract_rules Optional. extract () function checks whether the name of each key as valid variable name, as well as checking and variable names in the symbol table for conflicts. Illegal processing of digital keys and conflict will be determined based on this parameter. It can be one of the following values: EXTR_OVERWRITE- Default. If there is a conflict, it overwrites the existing variable. EXTR_SKIP- If there is a conflict, it does not overwrite the existing variable. (Ignore the array elements of the same name) EXTR_PREFIX_SAME- If there is a conflict, the variable name prefixed with prefix. Since PHP 4.0.5, which also includes the handling of digital index. EXTR_PREFIX_ALL- to all variable names prefixed with prefix (the third parameter). EXTR_PREFIX_INVALID- just before illegal or numeric variable names prefixed with prefix. This tag is added to the new PHP 4.0.5. EXTR_IF_EXISTS- only when the variable name already exists in the current symbol table, covering their values. The other is not treated. May be used in a combination of variables have been defined method, then, for example, from an array $ _REQUEST extracting only the case of these variables. This tag is added to the new PHP 4.2.0. EXTR_PREFIX_IF_EXISTS- When only the variable already exists in the current symbol table, establish additional variable name prefix, and the other is not treated. This tag is added to the new PHP 4.2.0. EXTR_REFS- the variable as a reference extract. This strongly suggests that the imported variables are still referencing the values of the parameters of var_array. This flag may be used alone or used in combination with any other OR extract_type flag. This tag is added in PHP 4.3.0.
prefix Optional. Note that prefix is ​​only the value extract_type EXTR_PREFIX_SAME, when EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS needs. If the result of the additional prefix is ​​not a valid variable name, you will not be imported into the symbol table. It will automatically add an underscore between the prefix and the array key.

demo

Pointing Code:

$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

Original = Original; Dog = Dog; Horse = Horse; Cat = Cat

Guess you like

Origin www.cnblogs.com/issac-fan/p/extract.html
Recommended