Introduction to various usages of extract functions in PHP

Source: Internet
Author: User

Directly on the code:

The code is as follows Copy Code
<?php
$my _array = Array ("A" => "Cat", "B" => "Dog", "C" => "horse");
Extract ($my _array);
echo "$a = $a; $b = $b; $c = $c ";
?>

Guess what will come out?

Output:

The code is as follows Copy Code
$a = Cat; $b = Dog; $c = Horse

No variable $a $b $c, no assignment, why can I get the value?


For example, can easily extract $_post or $_get elements, the form submitted to the content can not use one by one assignment, directly using the following code:

Form.html

The code is as follows Copy Code

<form action= "action.php" method= "POST" >
<input type= "text" name= "username" >
<input type= "password" name= "password" >
<input type= "Submit" >

Use the Extract () function in action.php to unlock the $_post global data:
action.php

The code is as follows Copy Code

<?php
Extract ($_post);
Equivalent to $username = $_post[' username '];
$password = $_post[' password '];
?>


Array operations


Related examples:

The code is as follows Copy Code

<?php

/* Assume that the $var _array is an array returned by Wddx_deserialize * *

$size = "large";
$var _array = Array ("Color" => "Blue",
"Size" => "medium",
"Shape" => "sphere");
Extract ($var _array, Extr_prefix_same, "WDDX");

echo "$color, $size, $shape, $wddx _sizen";

?>


The example above will output:

Blue, large, sphere, medium.
The $size is not covered because the extr_prefix_same is specified, which makes the $WDDX _size built. If Extr_skip is specified, the $WDDX _size will not be established. Extr_overwrite will make the value of $size "medium", Extr_prefix_all will create a new variable $wddx _color, $wddx _size and $wddx _shape.


Array 2


. Scope of application: Non-numeric index array, the array has the key value pairs;
2. The variable name created by the function is the keyword in the array, and the value of the variable is the corresponding value in the array;
3. Two optional parameters: Extract_type and prefix;
Where Extract_type specifies the method of handling variable name conflicts by default Extr_overwrite, which means overwriting existing variables.
When the value of the Extract_type is Extr_prefix_all, prefix all the created variables with a prefix of PREFIX parameters;
4. The keyword for the element in the array must be a valid variable name or it will be skipped.

Instance:

array_extract.php:

The code is as follows Copy Code

<title>test Array extract</title>
<body>
<?php
$array = Array (' Key1 ' => ' value1 ', ' key2 ' => ' value2 ', ' Key3 ' => ' value3 ');
Extract ($array);
echo ' default:<br/> ';
echo "$key 1 $key 2 $key 3<br/>";

Skip an element when a conflict occurs
$key 1 = "abc";
Extract ($array, extr_skip);
echo ' skip:<br/> ';
echo "$key 1 $key 2 $key 3<br/>";

Precede all variable names with the values provided by the prefix parameter
Extract ($array, Extr_prefix_all, ' my ');
echo ' prefix_all:<br/> ';
echo "$my _key1 $my _key2 $my _key3<br/>";
?>
</body>

is


Convenient?
about this function with an introduction to refer to http://www.111cn.net/phper/24/04ef3db43c8278b93cdd9203999b8352.htm

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.