Home  >  Article  >  Backend Development  >  PHP extract function to split an array into multiple variables_PHP tutorial

PHP extract function to split an array into multiple variables_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:36:401392browse

extract() function syntax:
int extract( array $var_array [, int $extract_type = EXTR_OVERWRITE [, string $prefix ]] )
Function: extract() function extracts associative array (for Numeric index array is invalid) For each pair of key and value, multiple sets of new variables are generated with key as the variable name and value as the corresponding value.

Copy code The code is as follows:

$size = "old size"; //Note The value of the final 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, we found that the value of $size is XXL, not the previous "old size", which means that by default, when the key in the array conflicts with an existing variable, the original variable will be overwritten.

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, no longer 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 variable name requires that the first character cannot be a number), add a prefix string to the variable name, and its value Specified by the third parameter.
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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322122.htmlTechArticleextract() function syntax: int extract( array $var_array [, int $extract_type = EXTR_OVERWRITE [, string $prefix ]] ) Function: The extract() function extracts associative arrays (nothing for numeric index arrays...
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