PHP extract() function usage php array splitting

WBOY
Release: 2016-07-25 08:54:09
Original
1295 people have browsed it
Copy code

In action.php, just use the extract() function to extract the $_POST global data: action.php

  1. extract($_POST);
  2. //Equivalent to $username = $_POST['username'];
  3. //$password = $_POST['password'];
  4. ?> ;
Copy code

Explanation in php manual:

extract (PHP 4, PHP 5)

extract — Import variables from an array into the current symbol table

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

This function is used to import variables from the array into the current symbol table. Accepts the associative array var_array as argument and uses the key name as the variable name and the value as the variable value. For each key/value pair a variable is created in the current symbol table, affected by the extract_type and prefix parameters.

Note: Since version 4.0.5 this function returns the number of variables extracted.

Note: EXTR_IF_EXISTS and EXTR_PREFIX_IF_EXISTS were introduced in version 4.2.0.

Note: EXTR_REFS was introduced in version 4.3.0.

extract() checks each key name to see if it can be used as a legal variable name, and also checks for conflicts with existing variable names in the symbol table. The treatment of illegal/numeric and conflicting key names will be determined by the extract_type parameter. Can be one of the following values:

  1. /* Assume $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_sizen”;
  8. ?>
Copy the code

The output of the above example: blue, large, sphere, medium

$size is not overridden because EXTR_PREFIX_SAME is specified, which causes $wddx_size to be built. If EXTR_SKIP is specified, $wddx_size will not be created either. EXTR_OVERWRITE will make $size have the value "medium" and EXTR_PREFIX_ALL will create new variables $wddx_color, $wddx_size and $wddx_shape.

Associative arrays must be used, numerically indexed arrays will not produce results unless EXTR_PREFIX_ALL or EXTR_PREFIX_INVALID is used.



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