当前位置: 首页 > 网络学院 > 服务端脚本教程 > PHP > extract() 函数

PHP
WINDOWS下安装MySQL
PHP 制作 网站/服务器 监视脚本
用PHP和CSS制作活动按钮
PHP 单件模式
PHP MVC模式,类封装以及HACK
PHP 中使用正则表达式
PHP 防止 SQL 注入攻击
PHP 跨站点脚本攻击
PHP 防止用户操纵 GET 变量
PHP 防止远程表单提交

PHP 中的 extract() 函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 370 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

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.
这个函数将返回所有被成功导入的变量。

Syntax
语法

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:
可用值:

  • EXTR_OVERWRITE - Default. On collision, the existing variable is overwritten
    EXTR_OVERWRITE – 默认值。当名称发生冲突时,当前变量将被覆盖
  • EXTR_SKIP - On collision, the existing variable is not overwritten
    EXTR_SKIP -当名称发生冲突时,不覆盖当前值
  • EXTR_PREFIX_SAME - On collision, the variable name will be given a prefix
    EXTR_PREFIX_SAME - 当名称发生冲突时,变量名称将会自动加上一个前缀
  • EXTR_PREFIX_ALL - All variable names will be given a prefix
    EXTR_PREFIX_ALL – 给所有的变量名称加上一个前缀
  • EXTR_PREFIX_INVALID - Only invalid or numeric variable names will be given a prefix
    EXTR_PREFIX_INVALID – 只给无效的或者数值型的名称加上前一个前缀
  • EXTR_IF_EXISTS - Only overwrite existing variables in the current symbol table, otherwise do nothing
    EXTR_IF_EXISTS – 只覆盖存在于当前的符号表(local symbol table)中现存变量
  • EXTR_PREFIX_IF_EXISTS - Only add prefix to variables if the same variable exists in the current symbol table
    EXTR_PREFIX_IF_EXISTS – 如果当前的符号表(local symbol table)中的变量相同,则在变量前面加上前缀
  • EXTR_REFS - Extracts variables as references. The imported variables are still referencing the values of the array parameter
    EXTR_REFS – 选取参考变量。导入的变量对数组参数的值进行标注
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.
这个参数指定了前缀。这个前缀将加在数组键之前,并在中间插入一条下划线(字符)来形成一个新的键名。



Example 1
案例1

<?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


Example 2
案例2

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;

评论 (0) All

登陆 | 还没注册?