Home > Backend Development > PHP Tutorial > The php extract() function converts the variables in the data into php variables_PHP tutorial

The php extract() function converts the variables in the data into php variables_PHP tutorial

WBOY
Release: 2016-07-20 11:03:12
Original
915 people have browsed it

The PHP extract() function imports variables from an array into the current symbol table. For each element in the array, the key name is used for the variable name and the key value is used for the variable value. The second parameter type is used to specify how the extract() function treats such conflicts when a variable already exists and there is an element with the same name in the array. ​

Export the variable from the PHP array and register it as a global variable, using the key name as the variable name and the value as the variable value, as follows

 代码如下 复制代码
$vars = array('var1'=>'1','var2'=>'2','var3'=>'3','var4'=>'4','var5'=>'5');

Achieve access through key names as variable names, such as: $var1, $var2

The first option: use PHP’s built-in extract() function, the method is as follows

 代码如下 复制代码
extract($vars);

Second option: Use foreach loop array to register as a global variable, the method is as follows

 代码如下 复制代码
foreach($vars as $k=>$v){
    $GLOBALS[$k] = $v;
}

The second solution is recommended because the extract() function has performance and security issues.

extract() function description

(PHP 3 >= 3.0.7, PHP 4, PHP 5)
extract -- Import variables from an array into the current symbol table

The code is as follows Copy code
 代码如下 复制代码

int extract ( array var_array [, int extract_type [, string prefix]] )

int extract (array var_array [, int extract_type [, string prefix]] )

This function is used to import variables from the array into the current symbol table. Accepts the associative array var_array as argument and uses the key name as the variable name and the value as the variable value. For each key/value pair a variable is created in the current symbol table, affected by the extract_type and prefix parameters.

Reference table

Parameters Description
array Required. Specifies the input to use.
extract_rules
参数 描述
array 必需。规定要使用的输入。
extract_rules

可选。extract() 函数将检查每个键名是否为合法的变量名,同时也检查和符号表中的变量名是否冲突。

对非法、数字和冲突的键名的处理将根据此参数决定。可以是以下值之一:

可能的值:

  • EXTR_OVERWRITE - 默认。如果有冲突,则覆盖已有的变量。
  • EXTR_SKIP - 如果有冲突,不覆盖已有的变量。(忽略数组中同名的元素)
  • EXTR_PREFIX_SAME - 如果有冲突,在变量名前加上前缀 prefix。自 PHP 4.0.5 起,这也包括了对数字索引的处理。
  • EXTR_PREFIX_ALL - 给所有变量名加上前缀 prefix(第三个参数)。
  • EXTR_PREFIX_INVALID - 仅在非法或数字变量名前加上前缀 prefix。本标记是 PHP 4.0.5 新加的。
  • EXTR_IF_EXISTS - 仅在当前符号表中已有同名变量时,覆盖它们的值。其它的都不处理。可以用在已经定义了一组合法的变量,然后要从一个数组例如 $_REQUEST 中提取值覆盖这些变量的场合。本标记是 PHP 4.2.0 新加的。
  • EXTR_PREFIX_IF_EXISTS - 仅在当前符号表中已有同名变量时,建立附加了前缀的变量名,其它的都不处理。本标记是 PHP 4.2.0 新加的。
  • EXTR_REFS - 将变量作为引用提取。这有力地表明了导入的变量仍然引用了 var_array 参数的值。可以单独使用这个标志或者在 extract_type 中用 OR 与其它任何标志结合使用。本标记是 PHP 4.3.0 新加的。
prefix

可选。请注意 prefix 仅在 extract_type 的值是 EXTR_PREFIX_SAME,EXTR_PREFIX_ALL,EXTR_PREFIX_INVALID 或 EXTR_PREFIX_IF_EXISTS 时需要。如果附加了前缀后的结果不是合法的变量名,将不会导入到符号表中。

前缀和数组键名之间会自动加上一个下划线。

Optional. The extract() function will check whether each key name is a legal variable name, and also checks whether it conflicts with the variable name in the symbol table.

The handling of illegal, numeric and conflicting key names will be determined based on this parameter. Can be one of the following values:

Possible values:
 代码如下 复制代码

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template