This article describes in detail the usage of the extract function in PHP, from changing data to variables, post, get data processing, and so on. This article describes in detail the usage of the extract function in PHP, from changing data to variables, post, get data processing, and so on.
Script ec (2); script
Directly run the Code:
The Code is as follows: |
|
$ My_array = array ("a" => "Cat", "B" => "Dog", "c" => "Horse "); Extract ($ my_array ); Echo "$ a = $ a; $ B = $ B; $ c = $ c "; ?> |
Guess what will happen?
Output:
The Code is as follows: |
|
$ A = Cat; $ B = Dog; $ c = Horse |
No variable $ a $ B $ c is defined, and no value is assigned. Why can we get the value?
For example, you can easily extract $ _ POST or $ _ GET elements. You cannot assign one-to-one 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
The Code is as follows: |
|
Extract ($ _ POST ); // Equivalent to $ username = $ _ POST ['username']; // $ Password = $ _ POST ['Password']; ?> |
Array Operations
Example:
The Code is as follows: |
|
/* Assume that $ var_array is the 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 above example will 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.
Array 2
. Applicability: Non-numeric index array with key-value pairs;
2. The variable name created by the function is the keyword in the array, and the variable value is the corresponding value in the array;
3. Two optional parameters: extract_type and prefix;
Extract_type specifies the method used to handle variable name conflicts. The default value is EXTR_OVERWRITE, indicating to overwrite existing variables,
When the value of extract_type is EXTR_PREFIX_ALL, the prefix is added before all created variables. The prefix is provided by the prefix parameter;
4. the keyword of the element in the array must be a valid variable name, otherwise it will be skipped.
Instance:
Array_extract.php:
The Code is as follows: |
|
Test Array Extract $ Array = array ('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3 '); Extract ($ array ); Echo 'default: '; Echo "$ key1 $ key2 $ key3 "; // Skip an element in case of a conflict $ Key1 = "abc "; Extract ($ array, EXTR_SKIP ); Echo 'skip: '; Echo "$ key1 $ key2 $ key3 ";
// Add the value provided by the prefix parameter before all variable names Extract ($ array, EXTR_PREFIX_ALL, 'My '); Echo 'prefix _ All: '; Echo "$ my_key1 $ my_key2 $ my_key3 "; ?>
|
Is it convenient?
For more information about this function, see http://www.111cn.net/phper/24/04ef3db43c8278b93cdd9203999b8352.htm.