Home > Backend Development > PHP Tutorial > extractdata PHP extract function to split an array into multiple variables

extractdata PHP extract function to split an array into multiple variables

WBOY
Release: 2016-07-29 08:43:12
Original
1542 people have browsed it

extract() function syntax:
int extract( array $var_array [, int $extract_type = EXTR_OVERWRITE [, string $prefix ]] )
Function: extract() function extracts each pair of key and value from an associative array (not valid for numeric index arrays) , generate multiple sets of new variables with key as the variable name and value as the corresponding value.

Copy the code The code is as follows:


$size = "old size"; //Pay attention to the value of the last size variable.
$a = array(
"color" => "red",
"size" => "XXL",
"price" => "53");
extract($a);
echo " color = $color
";
echo "size = $size
";
echo "price = $price
";
?>


The result is:
color = red
size = XXL
price = 53
Through the above example, it is found that the value of $size is XXL, not the previous "old size", indicating that by default, when the key in the array conflicts with an existing variable, it will Overwrite the original variable.
Continue to introduce the last two optional parameters of the extract function.
The second parameter $extract_type is used to control the processing method when a conflict occurs. Possible values ​​are:
EXTR_OVERWRITE: Overwrite existing variables in case of conflict, default value.
EXTR_SKIP: Do not overwrite existing variables, that is, do not generate variables for the key and value pair.
EXTR_PREFIX_SAME: In case of conflict, a prefix string is added to the generated new variable name, and its value is specified by the third parameter.
EXTR_PREFIX_ALL: Add a prefix string to all generated new variable names, the value of which is specified by the third parameter.
EXTR_PREFIX_INVALID: When it is invalid to convert the value of key into a variable name (for example, the first character of key is a number, and the first character of the variable name cannot be a number), add a prefix string to the variable name, and its value is determined by the first character. Three parameters are specified.
EXTR_IF_EXISTS: Only take out previously existing variables.
EXTR_PREFIX_IF_EXISTS: For the variable obtained by EXTR_IF_EXISTS, add a prefix string to the variable name, and its value is specified by the third parameter.
EXTR_REFS: Extract variables by reference, indicating that changes in the value of the extracted variable will affect the value of the original array.
Note: When adding a prefix string to the variable name, the new variable name is PREFIX_key, not PREFIXkey.

Copy code The code is as follows:


$a = array(
"color" => "red",
"size" => "XXL",
"price" => "53");
extract($a,EXTR_PREFIX_ALL,"SC");
echo "color = $SC_color
";
echo "size = $SC_size
";
echo "price = $SC_price
";
extract($a,EXTR_REFS);
$color="green";
echo $a['color']; //View the value of the original array
?> ;


The result is:
color = red
size = XXL
price = 53
green

The above introduces the extractdata PHP extract function that splits an array into multiple variables, including the extractdata aspect. I hope it will be helpful to friends who are interested in PHP tutorials.

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