PHPextract () function (array split) definition and usage

Source: Internet
Author: User
PHPextract () function (array split) definition and usage

  1. $ A = 'original ';
  2. $ My_array = array ("a" => "Cat", "B" => "Dog", "c" => "Horse ");
  3. Extract ($ my_array );
  4. Echo "\ $ a = $ a; \ $ B = $ B; \ $ c = $ c ";
  5. ?>

Output:

$ A = Cat; $ B = Dog; $ c = Horse

Example 2 use all parameters:

  1. $ A = 'original ';

  2. $ My_array = array ("a" => "Cat", "B" => "Dog", "c" => "Horse ");

  3. Extract ($ my_array, EXTR_PREFIX_SAME, 'dup ');

  4. Echo "\ $ a = $ a; \ $ B = $ B; \ $ c = $ c; \ $ dup_a = $ dup_a ;";

  5. ?>

Output: $ a = Original; $ B = Dog; $ c = Horse; $ dup_a = Cat;

PHP extract () function

Recently, when reading a cool man's code, I saw a very useful function: extract (). its main function is to expand the array and use the key name as the variable name, the element value is a variable value, which provides another convenient tool for Array Operations. for example, you can easily extract $ _ POST or $ _ GET elements, you cannot assign values to the content submitted in the form. you can directly use the following code:

Form.html

In action. php, you only need to use the extract () function to unbind the $ _ POST global data: action. php

  1. Extract ($ _ POST );
  2. // Equivalent to $ username = $ _ POST ['username'];
  3. // $ Password = $ _ POST ['password'];
  4. ?>

The following is a detailed explanation in the php manual:

Extract (PHP 4, PHP 5)

Extract-import the variable from the array to the current symbol table

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

This function is used to import variables from the array to the current symbol table. The array var_array is used as the parameter and the key name is used as the variable name, and the value is used as the variable value. Each key/value pair will create a variable in the current symbol table and be affected by the extract_type and prefix parameters.

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

Extract () checks each key name to see if it can be used as a valid variable name. it also checks for conflicts with existing variable names in the symbol table. The method for dealing with illegal/numbers and conflicting key names is determined by the extract_type parameter. It can be one of the following values:

EXTR_OVERWRITE overwrites existing variables if there is a conflict. EXTR_SKIP does not overwrite existing variables if there is a conflict. If EXTR_PREFIX_SAME conflict EXTR_PREFIX_SAME, add prefix before the variable name. EXTR_PREFIX_ALL adds the prefix to all variable names. Since PHP 4.0.5, this also includes processing digital indexes. EXTR_PREFIX_INVALID is prefixed only before the variable name of the invalid/number. This mark is newly added to PHP 4.0.5. EXTR_IF_EXISTS only overwrites the values of variables with the same name in the current symbol table. None of them are processed. It can be used for variables that have defined a combination, and then extract values from an array such as $ _ REQUEST to overwrite these variables. This mark is newly added to PHP 4.2.0. EXTR_PREFIX_IF_EXISTS creates a variable name with a prefix only when the variable with the same name already exists in the current symbol table. This mark is newly added to PHP 4.2.0. EXTR_REFS extracts variables as references. This effectively demonstrates that the imported variable still references the value of the var_array parameter. This flag can be used independently OR in extract_type OR with any other flag. This mark is newly added to PHP 4.3.0. If extract_type is not specified, it is assumed to be EXTR_OVERWRITE.

Note that prefix is only required when the value of extract_type is EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS. If the result with a prefix is not a valid variable name, it is not imported to the symbol table. An underline is automatically added between the prefix and the array key name.

Extract () returns the number of variables successfully imported into the symbol table.

Warning

Do not use extract () for untrusted data, such as user input ($ _ GET ,...). If you do this, for example, to temporarily run the old code dependent on register_globals, make sure that the extract_type value that will not be overwritten is used, such as EXTR_SKIP, and pay attention to php. the order defined by variables_order in ini is extracted.

One possible use of extract () is to import the content returned by wddx_deserialize () in the Combined Array to the symbol table variable.

Example:

  1. /* Assume that $ var_array is the array returned by wddx_deserialize */
  2. $ Size = "large ";
  3. $ Var_array = array ("color" => "blue ",
  4. "Size" => "medium ",
  5. "Shape" => "sphere ");
  6. Extract ($ var_array, EXTR_PREFIX_SAME, "wddx ");
  7. Echo "$ color, $ size, $ shape, $ wddx_size \ n ";
  8. ?>

Output: blue, large, sphere, medium

$ Size is not overwritten. because EXTR_PREFIX_SAME is specified, $ wddx_size is created. If EXTR_SKIP is specified, $ wddx_size is not created. EXTR_OVERWRITE sets the value of $ size to "medium", and EXTR_PREFIX_ALL creates a new variable $ wddx_color, $ wddx_size, and $ wddx_shape.

The associated array must be used. the array of the numeric index will not produce results unless EXTR_PREFIX_ALL or EXTR_PREFIX_INVALID is used.

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.