English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP extract() Function Usage and Example

PHP Array Function Manual

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

Syntax

extract($array, $extract_type, $prefix)

Definition and Usage

This function is used to import variables from an array into the current symbol table. It takes an associated array array, and treats the keys as variable names and the values as variable values. For each key/A value pair, it will create a variable in the current symbol table, constrained by the extract_type and prefix parameters.

Parameters

Serial NumberParameters and Description
1

array (required)

It specifies an array

2

extract_type (optional)

The extract() function checks invalid variable names and conflicts with existing variable names. This parameter specifies how to handle invalid names and conflict names. Possible values:

  • EXTR_OVERWRITE   -  If there is a conflict, overwrite the existing variables.

  • EXTR_SKIP   - If there is a conflict, do not overwrite the existing variables.

  • EXTR_PREFIX_SAME -  If there is a conflict, add the prefix prefix to the variable name.

  • EXTR_PREFIX_ALL -   Add the prefix prefix to all variable names.

  • EXTR_PREFIX_INVALID -  Only add the prefix prefix before illegal/digit variable names.

  • EXTR_IF_EXISTS -  Only overwrite the values of variables that already exist in the current symbol table when there are conflicts. This is very useful in the following scenario: define some valid variables, and then only import these defined variables from $_REQUEST.

  • EXTR_PREFIX_IF_EXISTS - Only create variable names with prefixes when there are already variables with the same name in the current symbol table, ignoring others.

  • EXTR_REFS  - Extract variables as references. This strongly indicates that the imported variables still reference the value of the array parameter. This flag can be used alone or combined with other flags using OR in flags.
    If flags are not specified, it is assumed to be EXTR_OVERWRITE.

3

prefix (optional)

If EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID, or EXTR_PREFIX_IF_EXISTS is used in the extract_rules parameter, the specified prefix is required.

This parameter specifies the prefix. The prefix is automatically separated from the array key by an underscore character.

Return Value

Returns the number of variables successfully imported into the symbol table.

Online Example

 "blue", "size" => "medium", "shape" => "sphere");
   extract(\$input, EXTR_PREFIX_SAME, "bbcx");
   echo "\$color, \$size, \$shape, \$bbcx_size";
?>
Test and see‹/›

Output Result:

blue, large, sphere, medium

   PHP Array Function Manual