当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > extract() 函数
The extract() function imports variables into the local symbol table from an array.
extract()函数的作用是:从数组中将变量导入到当前的符号表(local symbol table)中。
This function uses array keys as variable names and values as variable values. For each element it will create a variable in the current symbol table.
这个函数使用数组中元素的键作为导出后的变量的名称;使用数组中变量的值作为导出后变量的值。在当前的符号表(local symbol table)中,该函数将为每一个元素赋上一个变量。
This function returns the number of variables extracted on success.
这个函数将返回所有被成功导入的变量。
extract(array,extract_rules,prefix) |
Parameter参数 | Description描述 |
---|---|
array | Required. Specifies the array to use 必要参数。指定需要执行操作的数组对象 |
extract_rules | Optional. The extract() function checks for invalid variable names and collisions with existing variable names. This parameter specifies how invalid and colliding names are treated. 可选参数。extract()函数会检查无效的变量名以及现在变量名之间是否冲突(重复)。下面列举了具体可以使用的名称检查方式: Possible values:
|
prefix | Optional. If EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS are used in the extract_rules parameter, a specified prefix is required. 可选参数。如果EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID 或 EXTR_PREFIX_IF_EXISTS在extract_rules参数中使用,那必须要加上一个指定的前缀。 This parameter specifies the prefix. The prefix is automatically separated from the array key by an underscore character. |
<?php $a = 'Original'; $my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse"); extract($my_array); echo "$a = $a; $b = $b; $c = $c"; ?> |
The output of the code above will be:
上述代码将输出下面的结果:
$a = Cat; $b = Dog; $c = Horse |
With all parameters in use:
使用所有的参数:
<?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;"; ?> |
The output of the code above will be:
上述代码将输出下面的结果:
$a = Original; $b = Dog; $c = Horse; $dup_a = Cat; |