php

pHP EXTRACT () function to import variables into the current symbol table from the array
definition and usage

php extracrate () function introduces variables into the current symbol table from the array.

For each element in the array, the key name is used for variable names, and the key value is used for variable values.

The second parameter Type is used to specify how when a certain variable already exists, and when there is the same name elements in the array, the Extract () function treats such a conflict.

This function returns the number of variables that successfully set.

grammar
extract(array,extract_rules,prefix)

parameter description
array must. Input to be used.
extract_rules

optional. The Extract () function will check whether each key name is a legal variable name, and also check whether the variable names in the symbol table conflict.

Treatment of illegal, numbers and conflicts will be determined based on this parameter. It can be one of the following values:

Possible value:

  • EXTR_OVERWRITE -default. If there is a conflict, it will cover the existing variables.
  • EXTR_SKIP -If there is a conflict, it does not cover existing variables. (Ignore the element of the same name in the array)
  • EXTR_PREFIX_SAME -If there is a conflict, add prefix in front of the variable name. From PHP 4.0.5, this also includes the treatment of digital indexes.
  • EXTR_PREFIX_ALL -Add prefix (third parameter) to all variable names.
  • EXTR_PREFIX_INVALID -Add prefix prefix only before illegal or digital variable names. This label is newly added with PHP 4.0.5.
  • EXTR_IF_EXISTS -When there is a variable of the same name in the current symbol table, the value of them is covered. Nothing else is dealt with. It can be used for a set of legal variables, and then the extraction value is extracted from a array examples such as $ _Request to cover these variables. This mark is newly added with PHP 4.2.0.
  • EXTR_PREFIX_IF_EXISTS -When there is a variable of the same name in the current symbol table, a variable name with an additional prefix is established, and the others are not processed. This mark is newly added with PHP 4.2.0.
  • EXTR_REFS -Extract the variable as a reference. This strongly shows that the imported variable still quotes the value of the VAR_ARAY parameter. You can use this logo separately or use OR with any other logo in Extract_type. This label is newly added with PHP 4.3.0.
prefix

Optional. Please note that the value of Prefix is only when the value of the extract_type is EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS. If the result of the prefix is not a legal variable name, it will not be imported into the symbol table.

prefixes and array keys will automatically add a downward line.

Example 1

Copy codecode is as follows:
<?php
$a = ‘Original’;
$my_array = array(“a” => “Cat”,”b” => “Dog”, “c” => “Horse”);
extract($my_array);
echo “\$a = $a; \$b = $b; \$c = $c”;
?>

Output:

$a = Cat;
$b = Dog;
$c = Horse

Example 2

Use all parameters:

Copy codecode is as follows:
<?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;”;
?>

Output:

$a = Original;
$b = Dog;
$c = Horse;
$dup_a = Cat;

PHP EXTRACT () function

When looking at a cow’s code recently, I saw a very easy -to -use function: extract (). Its main function is to expand the array, the key name is the variable name, and the elemental value is the variable value. It can be said that it is an array of an array. The operation provides another convenient tool. For example, it can easily extract the elements of $ _post or $ _Get. The content submitted by the form must not be assigned one by one. Use the following code directly:

form.html

Copy codecode is as follows:
<form action=”action.php” method=”post”>
<input type=”text” name=”username”>
<input type=”password” name=”password”>
<input type=”submit”>

In ACTION.PHP, just use the extract () function to unlock the $ _post global data:
action.php

Copy codecode is as follows:
<?php
extract($_POST);
// equivalent to $ username = $ _post [‘username’];
//$password = $_POST[‘password’];
?>

Is it very convenient? Haha, the detailed explanation in the PHP manual below:

extract
(PHP 4, PHP 5)

Extract -imported variables from the array to the current symbol table

Instructions
int extract ( array $var_array [, int $extract_type [, string $prefix ]] )

This function is used to import variables from array into the current symbol table. Accept the combined array VAR_ARRAY as the parameter and use the key name as the variable name, and the value as the value of the variable. Each key / value pair will establish variables in the current symbol table and be affected by the Extract_type and Prefix parameters.

Note: From version 4.0.5, this function returns the number of variables extracted.
Note: EXTR_IF_EXISTS and EXTR_PREFIX_IF_EXISTS are introduced in the version 4.2.0.
Note: EXTR_REFS is introduced in the version 4.3.0.

Extract () Check whether each key name can be used as a legal variable name, and also check the conflict of the existing variable names in the symbol table. The method of treating illegal / numbers and conflicts will be determined based on the Extract_type parameter. It can be one of the following values:

EXTR_OVERWRITE
If there is a conflict, cover the existing variables.
EXTR_SKIP
If there are conflicts, do not cover existing variables.
EXTR_PREFIX_SAME
If there is a conflict, add prefix in front of the variable name.
EXTR_PREFIX_ALL
Add prefix prefix to all variable names. From PHP 4.0.5, this also includes the treatment of digital indexes.
EXTR_PREFIX_INVALID
In front of the illegal / digital variable name, add prefix. This label is newly added with PHP 4.0.5.
EXTR_IF_EXISTS
When there is a variable of the same name in the current symbolic table, they cover their values. Nothing else is dealt with. It can be used for a set of legal variables, and then the extraction value is extracted from a array examples such as $ _Request to cover these variables. This mark is newly added with PHP 4.2.0.
EXTR_PREFIX_IF_EXISTS
When there is a variable of the same name in the current symbol table, the variable names with prefix are added, and the others are not processed. This mark is newly added with PHP 4.2.0.
EXTR_REFS
Put the variable as a reference. This strongly shows that the imported variable still quotes the value of the VAR_ARAY parameter. You can use this logo separately or use OR with any other logo in Extract_type. This label is newly added with PHP 4.3.0.
If you do not specify the Extract_type, it is assumed that EXTR_OVERWRITE is assumed.

Note that Prefix is only required for EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS. If the result of the prefix is not a legal variable name, it will not be imported into the symbol table. There will be a downward line between the prefix and the array key name.

Extract () Returns the number of variables that successfully imported to the symbol table.

Warning

Do not use extract () that cannot be trusted, such as the user’s input ($ _get, …). If this is done, for example, it is necessary to temporarily run the old code that depends on the register_globals, and ensure that the extract_type value that will not be covered can be used, such as extr_skip, and pay attention to the order defined by variables_order in php.ini.

The possible usage of

extract () is to import the contents of the binding combination of wddx_deserialize () into the symbol table variable in the array.

Example# 1 extract () Example

Copy codecode is as follows:
<?php
/* Assume that $ var_array is an array returned by wddx_deserialize* /
$size = “large”;
$var_array = array(“color” => “blue”,
“size” => “medium”,
“shape” => “sphere”);
extract($var_array, EXTR_PREFIX_SAME, “wddx”);
echo “$color, $size, $shape, $wddx_size\n”;
?>

The above example will be output:

blue, large, sphere, medium

$ SIZE is not covered, because the ExtR_PREFIX_SAME is specified, which makes $ wddx_size establish. If EXTR_SKIP is specified, $ wddx_size will not be established. Extr_overWrite will make the value of $ SIZE as “Medium”, and EXTR_PREFIX_ALL will establish a new variable $ WDDX_COLOR, $ WDDX_SIZE and $ WDDX_SHAPE.

must use associated arrays, and the array of digital index will not produce results, unless EXTR_PREFIX_ALL or EXTR_PREFIX_INVALID.

Leave a Reply

Your email address will not be published. Required fields are marked *