Detailed explanation and example code of php array processing function extract_php example

WBOY
Release: 2023-03-03 07:20:02
Original
1264 people have browsed it

php array processing function extract

The extract function is used to import variables from an array into the current symbol table

Basic Grammar

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:

Parameters 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 - Prefix all variable names with prefix.
  • EXTR_PREFIX_INVALID - Only prefix illegal or numeric variable names.
  • EXTR_IF_EXISTS - Only overwrite the values ​​of variables with the same name if they already exist in the current symbol table. Others are not processed.
  • EXTR_PREFIX_IF_EXISTS - Only when there is a variable with the same name in the current symbol table, the variable name with the prefix is ​​created, and the others are not 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:

<&#63;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/>";
&#63;>  
Copy after login

Run result:

blue, large, sphere, medium

Thanks for reading, I hope it can help you, thank you for your support of this site!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!