PHP extract () function
Examples
The key "Cat", "Dog" and "Horse" is assigned to the variable $ 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";
?>
$a = "Original";
$my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse");
extract($my_array);
echo "$a = $a; $b = $b; $c = $c";
?>
Running instance »
Definition and Usage
extract () function to import an array of variables into the current symbol table.
This function uses the array key as a variable name, use the array as a key variable. For each element in the array will create a corresponding variable in the current symbol table.
The function returns the number of variables successfully set.
grammar
extract( array,extract_rules,prefix )
参数 | 描述 |
---|---|
array | 必需。规定要使用的数组。 |
extract_rules | 可选。extract() 函数将检查每个键名是否为合法的变量名,同时也检查和符号表中已存在的变量名是否冲突。对不合法和冲突的键名的处理将根据此参数决定。 可能的值:
|
prefix | 可选。如果 extract_rules 参数的值是 EXTR_PREFIX_SAME、EXTR_PREFIX_ALL、
EXTR_PREFIX_INVALID 或 EXTR_PREFIX_IF_EXISTS,则 prefix 是必需的。
该参数规定了前缀。前缀和数组键名之间会自动加上一个下划线。 |
technical details
return value: | Returns the number of variables successfully set. |
---|---|
PHP version: | 4+ |
Update log: | extract_rules value EXTR_REFS is new in PHP 4.3. extract_rules value EXTR_IF_EXISTS and EXTR_PREFIX_IF_EXISTS is new in PHP 4.2. Since PHP 4.0.5, this function returns the number of variables successfully set. extract_rules value EXTR_PREFIX_INVALID is new in PHP 4.0.5. Since PHP 4.0.5, extract_rules value EXTR_PREFIX_ALL also includes numeric variables. |
More examples
Example 1
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";
?>
$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";
?>
Running instance »
Complete PHP Array Reference