PHPextract: function for splitting arrays into multiple variables _ PHP Tutorial

Source: Internet
Author: User
PHPextract is a function that splits an array into multiple variables. Extract () function syntax: intextract (array $ var_array [, int $ extract_typeEXTR_OVERWRITE [, string $ prefix]) function: the extract () function extracts correlated arrays (no numeric index array Extract () function syntax:
Int extract (array $ var_array [, int $ extract_type = EXTR_OVERWRITE [, string $ prefix])
Function: the extract () function extracts the associated array (invalid for the number index array). each pair of key and value generates multiple groups of new variables with the key as the variable name and value as the corresponding value.

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 ($ );
Echo "color = $ color
";
Echo "size = $ size
";
Echo "price = $ price
";
?>



Result:

Color = red
Size = XXL
Price = 53

In the previous example, we found that the value of $ size is XXL, instead of the previous "old size". This indicates 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. the possible value is:

EXTR_OVERWRITE: overwrites existing variables in case of conflict. default value.
EXTR_SKIP: the existing variables are not overwritten, that is, the variables of the key and value pairs are not generated.
EXTR_PREFIX_SAME: when a conflict occurs, the new variable name is added with a prefix string, which is specified by the third parameter.
EXTR_PREFIX_ALL: Adds a prefix string to all generated variable names. The value is specified by the third parameter.
EXTR_PREFIX_INVALID: it is invalid when the key value is changed to the variable name (for example, the first character of the key is a number, and the first character of the variable name must not be a number). add a prefix string to the variable name, the value is specified by the third parameter.
EXTR_IF_EXISTS: only existing variables are retrieved.
EXTR_PREFIX_IF_EXISTS: add a prefix string to the variable obtained by EXTR_IF_EXISTS. The value is specified by the third parameter.
EXTR_REFS: extracts variables as a reference, indicating that the value of the extracted variable changes, which affects the value of the original array.

Note: When a variable name is prefixed with a string, the new variable name is PREFIX_key instead of PREFIXkey.

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
?>


Result:
Color = red
Size = XXL
Price = 53
Green

Http://www.bkjia.com/PHPjc/322122.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/322122.htmlTechArticleextract () function syntax: int extract (array $ var_array [, int $ extract_type = EXTR_OVERWRITE [, string $ prefix]) function: extract () function extraction associated array (no numeric index array...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.