PHP extract() Function
Complete PHP Array Reference Manual
Example
Assign the values "Cat", "Dog", and "Horse" to the variables $a, $b, and $c:
" Cat " , " b " => " Dog " , " c " => " Horse " ) ; extract ( $my_array ) ; echo " \$ a = $a ; \$ b = $b ; \$ c = $c " ; ?>Run Example »
Definition and Usage
The extract() function imports variables from an array into the current symbol table.
This function uses array keys as variable names and array values as variable values. For each element in the array, a corresponding variable is created in the current symbol table.
This function returns the number of variables successfully set.
Syntax
extract(
array,extract_rules,prefix
)
| Parameters | Description |
|---|---|
| array | Required. Specifies the array to use. |
| extract_rules | Optional. The extract() function checks whether each key is a valid variable name and also checks for conflicts with variables already in the symbol table. How invalid and conflicting keys are handled is determined by this parameter.
Possible values: |
该参数Specifies了前缀。前缀和数组键名之间会自动加上一个下划线。 |
Technical Details
| Return Value: | Returns the number of variables successfully set. |
|---|---|
| PHP Version: | 4+ |
| Changelog: | extract_rules The value EXTR_REFS was added in PHP 4.3. |
*extract_rules* 的值 EXTR_IF_EXISTS 和 EXTR_PREFIX_IF_EXISTS 是在 PHP 4.2 中新增的。
自 PHP 4.0.5 起,该函数返回成功设置的变量数目。
*extract_rules* 的值 EXTR_PREFIX_INVALID 是在 PHP 4.0.5 中新增的。
自 PHP 4.0.5 起,*extract_rules* 的值 EXTR_PREFIX_ALL 也包含数字变量。 |
More examples
Example 1
Using all parameters:
"Cat", "b" => "Dog", "c" => "Horse"); extract($my_array, EXTR_PREFIX_SAME, "dup"); echo "$a = $a; $b = $b; $c = $c; $dup_a = $dup_a"; ?>Run Example »
Complete PHP Array Reference Manual