Home  >  Article  >  Backend Development  >  Detailed explanation and examples of PHP array processing function extract

Detailed explanation and examples of PHP array processing function extract

墨辰丷
墨辰丷Original
2018-05-30 15:55:161867browse

php extract function uses the array key name as the variable name and the array key value as the variable value. This function can handle form submission and insert into the database. The article explains to you the basic usage and examples of the extract function. Friends who need it can refer to

php array processing function extract

extract function is used to extract data from an array. Variables are imported into the current symbol table

Basic syntax

int extract ( array &$var_array [, int $extract_type = EXTR_OVERWRITE [, string $prefix = NULL ]] )

This function is used to import variables from the array into the current symbol table. Each key name is checked to see if it can be used as a legal variable name, and is also checked for conflicts with existing variable names in the symbol table.

Parameter introduction:


Parameter Description
var_array Required. Specifies the array to use.

An associative array. This function treats the key name as the variable name and the value as the variable's value. For each key/value pair a variable is created in the current symbol table, affected by the extract_type and prefix parameters.

Associative arrays must be used, numerically indexed arrays will not produce results unless EXTR_PREFIX_ALL or EXTR_PREFIX_INVALID is used.

extract_type

Optional. The extract() function will check whether each key name is a legal variable name, and also checks whether it conflicts with an existing variable name in the symbol table. The handling of illegal and conflicting key names will be determined based on this parameter.

Possible values:

  • EXTR_OVERWRITE - Default. If there is a conflict, existing variables are overwritten.

  • EXTR_SKIP - Do not overwrite existing variables if there is a conflict.

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

  • EXTR_PREFIX_ALL - Add prefix to all variable names.

  • EXTR_PREFIX_INVALID - Prefix only illegal or numeric variable names with prefix.

  • EXTR_IF_EXISTS - Overwrite the values ​​of variables with the same name only if they already exist in the current symbol table. Others are not processed.

  • EXTR_PREFIX_IF_EXISTS - Only when a variable with the same name already exists in the current symbol table, a variable name with a prefix is ​​created, and nothing else is processed.

  • EXTR_REFS - Extract variables as references. The imported variable still references the value of the array parameter.

prefix

Optional. Note that prefix is ​​only required if the value of extract_type is EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS. If the result after appending the prefix is ​​not a legal variable name, it will not be imported into the symbol table. An underscore is automatically added between the prefix and the array key name.

Return value

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

Example:

<?php
$size = "large";
$var_array = array(
  "color" => "blue",
  "size" => "medium",
  "shape" => "sphere"
);
extract($var_array, EXTR_PREFIX_SAME, "wddx");
echo " $color , $size , $shape , $wddx_size <br/>";
?>

Run result:

blue, large, sphere, medium

and above That’s the entire content of this article, I hope it will be helpful to everyone’s study.


##Related recommendations:

PHP realizes third-party instant access to logistics updates

PHP method to implement multi-dimensional array sorting according to a certain key value

PHP method to implement email sending instance based on SMTP protocol

The above is the detailed content of Detailed explanation and examples of PHP array processing function extract. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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