Assign the key values "Cat", "Dog" and "Horse" to the variables $a, $b and $c:
<?php $a = " Original " ; $my_array = array ( " a " => " Cat " , " b " => " Dog " , " c " => " Horse " ) ; extract ( $my_array ) ; echo " $ a = $a ; $ b = $b ; $ c = $c " ; ?>The extract() function imports variables from an array into the current symbol table.
This function uses the array key name as the variable name and the array key value as the variable value. For each element in the array, a corresponding variable will be created in the current symbol table.
This function returns the number of variables successfully set.
extract( array,extract_rules,prefix )
parameter | describe |
---|---|
array | Required. Specifies the array to use. |
extract_rules | Optional. The extract() function will check whether each key name is a legal variable name and also checks whether it conflicts with an existing variable name in the symbol table. The handling of illegal and conflicting key names will be determined based on this parameter. Possible values: EXTR_OVERWRITE - Default. If there is a conflict, existing variables are overwritten. EXTR_SKIP - Do not overwrite existing variables if there is a conflict. EXTR_PREFIX_SAME - Prefix the variable name with prefix if there is a conflict. EXTR_PREFIX_ALL - Add prefix to all variable names. EXTR_PREFIX_INVALID - Prefix only illegal or numeric variable names. EXTR_IF_EXISTS - Overwrite the values of variables with the same name only if they already exist in the current symbol table. Others are not processed. EXTR_PREFIX_IF_EXISTS - Create a variable name with a prefix appended only if a variable with the same name already exists in the current symbol table. Others will not be processed. EXTR_REFS - Extract variables as references. The imported variable still references the value of the array parameter. |
prefix | Optional. The prefix is required if the value of the extract_rules parameter is EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID, or EXTR_PREFIX_IF_EXISTS. This parameter specifies the prefix. An underscore is automatically added between the prefix and the array key name. |
Return value: | Returns the number of successfully set variables. |
---|---|
PHP version: | 4+ |
Update log: | The value of extract_rules EXTR_REFS is new in PHP 4.3. The values of extract_rules EXTR_IF_EXISTS and EXTR_PREFIX_IF_EXISTS are new in PHP 4.2. As of PHP 4.0.5, this function returns the number of variables successfully set. The value of extract_rules EXTR_PREFIX_INVALID is new in PHP 4.0.5. Since PHP 4.0.5, the value of extract_rules EXTR_PREFIX_ALL also contains numeric variables. |
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";?>