Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP extract() Function

PHP extract() Function

PHP Array ReferenceComplete PHP Array Reference Manual

Example

Assign the values "Cat", "Dog", and "Horse" to the variables $a, $b, and $c:

" Cat " , " b " => " Dog " , " c " => " Horse " ) ; extract ( $my_array ) ; echo " \$ a = $a ; \$ b = $b ; \$ c = $c " ; ?>

Run Example »


Definition and Usage

The extract() function imports variables from an array into the current symbol table.

This function uses array keys as variable names and array values as variable values. For each element in the array, a corresponding variable is created in the current symbol table.

This function returns the number of variables successfully set.


Syntax

extract(

array,extract_rules,prefix

)

Parameters Description
array Required. Specifies the array to use.
extract_rules Optional. The extract() function checks whether each key is a valid variable name and also checks for conflicts with variables already in the symbol table. How invalid and conflicting keys are handled is determined by this parameter.

Possible values:

  • EXTR_OVERWRITE - Default. On conflict, overwrite the existing variable.
  • EXTR_SKIP - On conflict, do not overwrite the existing variable.
  • EXTR_PREFIX_SAME - On conflict, prefix the variable name with prefix.
  • EXTR_PREFIX_ALL - Prefix all variable names with prefix.
  • EXTR_PREFIX_INVALID - Prefix only invalid or numeric variable names with prefix.
  • EXTR_IF_EXISTS - Overwrite the value only if a variable of the same name already exists in the current symbol table. Nothing else is done.
  • EXTR_PREFIX_IF_EXISTS - Create prefixed variable names only if a variable of the same name already exists in the current symbol table. Nothing else is done.
  • EXTR_REFS - Extracts variables as references. The imported variables still refer to the values of the array parameter.
  • | | *prefix* | Optional. If the extract_rules parameter is EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID, or EXTR_PREFIX_IF_EXISTS, then prefix is required.
     该参数Specifies了前缀。前缀和数组键名之间会自动加上一个下划线。 |
    

    Technical Details

    Return Value: Returns the number of variables successfully set.
    PHP Version: 4+
    Changelog: extract_rules The value EXTR_REFS was added in PHP 4.3.
     *extract_rules* 的值 EXTR_IF_EXISTS 和 EXTR_PREFIX_IF_EXISTS 是在 PHP 4.2 中新增的。       
       
     自 PHP 4.0.5 起,该函数返回成功设置的变量数目。       
       
     *extract_rules* 的值 EXTR_PREFIX_INVALID 是在 PHP 4.0.5 中新增的。       
       
     自 PHP 4.0.5 起,*extract_rules* 的值 EXTR_PREFIX_ALL 也包含数字变量。 |
    

    More examples

    Example 1

    Using all parameters:

    "Cat", "b" => "Dog", "c" => "Horse"); extract($my_array, EXTR_PREFIX_SAME, "dup"); echo "$a = $a; $b = $b; $c = $c; $dup_a = $dup_a"; ?>

    Run Example »


    PHP Array ReferenceComplete PHP Array Reference Manual