1)
小结
类比golang build 时的版本参数
extract
(PHP 4, PHP 5, PHP 7, PHP 8)
extract — Import variables into the current symbol table from an array
Description
&$array
, int $flags
= EXTR_OVERWRITE
, string $prefix
= ""): intImport variables from an array into the current symbol table.
Checks each key to see whether it has a valid variable name. It also checks for collisions with existing variables in the symbol table.
Parameters
array
-
An associative array. This function treats keys as variable names and values as variable values. For each key/value pair it will create a variable in the current symbol table, subject to
flags
andprefix
parameters.You must use an associative array; a numerically indexed array will not produce results unless you use
EXTR_PREFIX_ALL
orEXTR_PREFIX_INVALID
. flags
-
The way invalid/numeric keys and collisions are treated is determined by the extraction
flags
. It can be one of the following values:EXTR_OVERWRITE
- If there is a collision, overwrite the existing variable.
EXTR_SKIP
- If there is a collision, don't overwrite the existing variable.
EXTR_PREFIX_SAME
- If there is a collision, prefix the variable name with
prefix
. EXTR_PREFIX_ALL
- Prefix all variable names with
prefix
. EXTR_PREFIX_INVALID
- Only prefix invalid/numeric variable names with
prefix
. EXTR_IF_EXISTS
- Only overwrite the variable if it already exists in the current symbol table, otherwise do nothing. This is useful for defining a list of valid variables and then extracting only those variables you have defined out of $_REQUEST, for example.
EXTR_PREFIX_IF_EXISTS
- Only create prefixed variable names if the non-prefixed version of the same variable exists in the current symbol table.
EXTR_REFS
- Extracts variables as references. This effectively means that the values of the imported variables are still referencing the values of the
array
parameter. You can use this flag on its own or combine it with any other flag by OR'ing theflags
.
If
flags
is not specified, it is assumed to beEXTR_OVERWRITE
. prefix
-
Note that
prefix
is only required ifflags
isEXTR_PREFIX_SAME
,EXTR_PREFIX_ALL
,EXTR_PREFIX_INVALID
orEXTR_PREFIX_IF_EXISTS
. If the prefixed result is not a valid variable name, it is not imported into the symbol table. Prefixes are automatically separated from the array key by an underscore character.
Return Values
Returns the number of variables successfully imported into the symbol table.
Examples
Example #1 extract() example
A possible use for extract() is to import into the symbol table variables contained in an associative array returned by wddx_deserialize().
<?php
/* Suppose that $var_array is an array returned from
wddx_deserialize */
$size = "large";
$var_array = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");
extract($var_array, EXTR_PREFIX_SAME, "wddx");
echo "$color, $size, $shape, $wddx_size\n";
?>
The above example will output:
blue, large, sphere, medium
The $size wasn't overwritten because we specified EXTR_PREFIX_SAME
, which resulted in $wddx_size being created. If EXTR_SKIP
was specified, then $wddx_size wouldn't even have been created. EXTR_OVERWRITE
would have caused $size to have value "medium", and EXTR_PREFIX_ALL
would result in new variables being named $wddx_color, $wddx_size, and $wddx_shape.
Notes
Do not use extract() on untrusted data, like user input (i.e. $_GET, $_FILES, etc.). If you do, make sure you use one of the non-overwriting flags
values such as EXTR_SKIP
and be aware that you should extract in the same order that's defined in variables_order within the php.ini.
See Also
PHP: extract - Manual https://www.php.net/manual/zh/function.extract.php
<?php
/* 假定 $var_array 是 wddx_deserialize 返回的数组*/
$size = "large";
$var_array = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");
extract($var_array, EXTR_PREFIX_SAME, "wddx");
echo "$color, $size, $shape, $wddx_size\n";
?>
以上例程会输出:
blue, large, sphere, medium
extract
(PHP 4, PHP 5, PHP 7, PHP 8)
extract — 从数组中将变量导入到当前的符号表
说明
&$array
, int $flags
= EXTR_OVERWRITE
, string $prefix
= ""): int本函数用来将变量从数组中导入到当前的符号表中。
检查每个键名看是否可以作为一个合法的变量名,同时也检查和符号表中已有的变量名的冲突。
参数
array
-
一个关联数组。此函数会将键名当作变量名,值作为变量的值。 对每个键/值对都会在当前的符号表中建立变量,并受到
flags
和prefix
参数的影响。必须使用关联数组,数字索引的数组将不会产生结果,除非用了
EXTR_PREFIX_ALL
或者EXTR_PREFIX_INVALID
。 flags
-
对待非法/数字和冲突的键名的方法将根据取出标记
flags
参数决定。可以是以下值之一:EXTR_OVERWRITE
- 如果有冲突,覆盖已有的变量。
EXTR_SKIP
- 如果有冲突,不覆盖已有的变量。
EXTR_PREFIX_SAME
- 如果有冲突,在变量名前加上前缀
prefix
。 EXTR_PREFIX_ALL
- 给所有变量名加上前缀
prefix
。 EXTR_PREFIX_INVALID
- 仅在非法/数字的变量名前加上前缀
prefix
。 EXTR_IF_EXISTS
- 仅在当前符号表中已有同名变量时,覆盖它们的值。其它的都不处理。 举个例子,以下情况非常有用:定义一些有效变量,然后从 $_REQUEST 中仅导入这些已定义的变量。
EXTR_PREFIX_IF_EXISTS
- 仅在当前符号表中已有同名变量时,建立附加了前缀的变量名,其它的都不处理。
EXTR_REFS
- 将变量作为引用提取。这有力地表明了导入的变量仍然引用了
array
参数的值。可以单独使用这个标志或者在flags
中用 OR 与其它任何标志结合使用。
如果没有指定
flags
,则被假定为EXTR_OVERWRITE
。 prefix
-
注意
prefix
仅在flags
的值是EXTR_PREFIX_SAME
,EXTR_PREFIX_ALL
,EXTR_PREFIX_INVALID
或EXTR_PREFIX_IF_EXISTS
时需要。 如果附加了前缀后的结果不是合法的变量名,将不会导入到符号表中。前缀和数组键名之间会自动加上一个下划线。
返回值
返回成功导入到符号表中的变量数目。
范例
示例 #1 extract() 例子
extract() 的一种可能用法是将 wddx_deserialize() 返回的结合数组中的内容导入到符号表变量中去。
<?php
/* 假定 $var_array 是 wddx_deserialize 返回的数组*/
$size = "large";
$var_array = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");
extract($var_array, EXTR_PREFIX_SAME, "wddx");
echo "$color, $size, $shape, $wddx_size\n";
?>
以上例程会输出:
blue, large, sphere, medium
$size 没有被覆盖,因为指定了 EXTR_PREFIX_SAME
,这使得 $wddx_size 被建立。如果指定了 EXTR_SKIP
,则 $wddx_size 也不会被建立。EXTR_OVERWRITE
将使 $size 的值为“medium”,EXTR_PREFIX_ALL
将建立新变量 $wddx_color,$wddx_size 和 $wddx_shape。
注释
不要对不能信任的数据使用 extract(),例如用户的输入($_GET, $_FILES...)。 如果这样做,要确保使用不会覆盖的 flags
值,例如 EXTR_SKIP
,并且要留意应该按照 variables_order 在 php.ini 里 定义的顺序来提取。