PHP extract() function

Example

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

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

Run Instances

Definition and Usage

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

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

Second parameter type Used to specify how the extract() function should handle conflicts when a variable already exists and there is a matching element in the array.

This function returns the number of variables successfully imported into the symbol table.

Syntax

extract(array,extract_rules,prefix)
Parameter Description
array Required. Specifies the array to be used.
extract_rules

Optional. The extract() function will check if each key name is a valid variable name and also check if there is a conflict with the existing variable names in the symbol table. The handling of invalid and conflicting key names will be determined by this parameter.

Possible values:

  • EXTR_OVERWRITE - Default. Overwrite existing variables if there is a conflict
  • EXTR_SKIP - Do not overwrite existing variables if there is a conflict
  • EXTR_PREFIX_SAME - Add a prefix to variable names if there is a conflict prefix.
  • EXTR_PREFIX_ALL - Add a prefix to all variable names prefix.
  • EXTR_PREFIX_INVALID - Only add a prefix before invalid or numeric variable names prefix.
  • EXTR_IF_EXISTS - Only overwrite the values of variables that already exist in the current symbol table. Do not process the others.
  • EXTR_PREFIX_IF_EXISTS - Only create a variable name with a prefix if there is already a variable with the same name in the current symbol table. Do not process the others.
  • EXTR_REFS - Extract variables as references. The imported variables still refer to the value of the array parameter.
prefix

Optional. Please note prefix Only extract_type Is needed when the value is EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID, or EXTR_PREFIX_IF_EXISTS. If the result after adding the prefix is not a valid variable name, it will not be imported into the symbol table.

An underscore is automatically added between the prefix and the array key name.

Technical Details

Return Value: Returns the number of variables successfully imported into the symbol table.
PHP Version: 4+
Update Log:

extract_rules The value EXTR_REFS is added in PHP 4.3.

extract_rules The values EXTR_IF_EXISTS and EXTR_PREFIX_IF_EXISTS are added in PHP 4.2.

Since PHP 4.0.5, this function returns the number of variables successfully imported into the symbol table.

extract_rules The value EXTR_PREFIX_INVALID is added in PHP 4.0.5.

Since PHP 4.0.5,extract_rules The value EXTR_PREFIX_ALL also includes numeric variables.

More Examples

Example 1

Use all parameters:

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

Run Instances