当前位置:网站首页>PHP extract() function

PHP extract() function

2022-06-24 05:11:00 User 1448800

example

Will be the key value "Cat"、"Dog" and "Horse" Assign a value to a variable $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";
?>

Definition and Usage

extract() Function to import variables from an array into the current symbol table .

This function uses the array key name as the variable name , Use array key value as variable value . For each element in the array , A corresponding variable will be created in the current symbol table .

The second parameter type Used to specify when a variable already exists , When there are elements with the same name in the array ,extract() How functions deal with such conflicts .

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

grammar

extract(array,extract_rules,prefix)

Parameters

describe

array

It's necessary . Specify the array to use .

extract_rules

Optional .extract() The function checks that each key name is a valid variable name , It also checks whether it conflicts with the existing variable names in the symbol table . Handling illegal and conflicting key names will be determined by this parameter . Possible value :EXTR_OVERWRITE - Default . If there is a conflict , Overwrite existing variables .EXTR_SKIP - If there is a conflict , Do not overwrite existing variables .EXTR_PREFIX_SAME - If there is a conflict , Prefix variable names  prefix.EXTR_PREFIX_ALL - Prefix all variable names  prefix.EXTR_PREFIX_INVALID - Prefix only illegal or numeric variable names  prefix.EXTR_IF_EXISTS - Only when a variable with the same name already exists in the current symbol table , Override their values . Nothing else .EXTR_PREFIX_IF_EXISTS - Only when a variable with the same name already exists in the current symbol table , Create variable names with prefixes attached , Nothing else .EXTR_REFS - Extract variables as references . The imported variable still references the value of the array parameter .

prefix

Optional . Please note that  prefix  Only in  extract_type  The value of is EXTR_PREFIX_SAME,EXTR_PREFIX_ALL,EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS The need when . If the result after prefixing is not a legal variable name , Will not be imported into the symbol table . An underscore is automatically added between the prefix and the array key name .

  • EXTR_OVERWRITE - Default . If there is a conflict , Overwrite existing variables .
  • EXTR_SKIP - If there is a conflict , Do not overwrite existing variables .
  • EXTR_PREFIX_SAME - If there is a conflict , Prefix variable names prefix.
  • EXTR_PREFIX_ALL - Prefix all variable names prefix.
  • EXTR_PREFIX_INVALID - Prefix only illegal or numeric variable names prefix.
  • EXTR_IF_EXISTS - Only when a variable with the same name already exists in the current symbol table , Override their values . Nothing else .
  • EXTR_PREFIX_IF_EXISTS - Only when a variable with the same name already exists in the current symbol table , Create variable names with prefixes attached , Nothing else .
  • EXTR_REFS - Extract variables as references . The imported variable still references the value of the array parameter .

prefix Optional . Please note that prefix Only in extract_type The value of is EXTR_PREFIX_SAME,EXTR_PREFIX_ALL,EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS The need when . If the result after prefixing is not a legal variable name , 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 edition :

4+

Update log :

extract_rules  Value EXTR_REFS Is in PHP 4.3 In the new .extract_rules  Value EXTR_IF_EXISTS and EXTR_PREFIX_IF_EXISTS Is in PHP 4.2 In the new . since PHP 4.0.5 rise , This function returns the number of variables successfully imported into the symbol table .extract_rules  Value EXTR_PREFIX_INVALID Is in PHP 4.0.5 In the new . since PHP 4.0.5 rise ,extract_rules  Value EXTR_PREFIX_ALL It also contains numeric variables .

More instances

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";
?>
原网站

版权声明
本文为[User 1448800]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/08/20210821105037122o.html