Home > Article > Backend Development > Detailed explanation and example code of php array processing function extract_php example
php array processing function extract
The extract function is used to import variables from an array into the current symbol table
Basic Grammar
int extract ( array &$var_array [, int $extract_type = EXTR_OVERWRITE [, string $prefix = NULL ]] )
This function is used to import variables from the array into the current symbol table. Each key name is checked to see if it can be used as a legal variable name, and is also checked for conflicts with existing variable names in the symbol table.
Parameter introduction:
Parameters | Description |
---|---|
var_array | Required. Specifies the array to use.
An associative array. This function treats the key name as the variable name and the value as the variable's value. For each key/value pair a variable is created in the current symbol table, affected by the extract_type and prefix parameters. Associative arrays must be used, numerically indexed arrays will not produce results unless EXTR_PREFIX_ALL or EXTR_PREFIX_INVALID is used. |
extract_type |
Optional. The extract() function will check whether each key name is a legal variable name, and also checks whether it conflicts with an existing variable name in the symbol table. The handling of illegal and conflicting key names will be determined based on this parameter. Possible values:
|
prefix |
Optional. Note that prefix is only required if the value of extract_type is EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS. If the result after appending the prefix is not a legal variable name, it will not be imported into the symbol table. An underscore is automatically added between the prefix and the array key name. |
Return value
Returns the number of variables successfully imported into the symbol table.
Example:
<?php $size = "large"; $var_array = array( "color" => "blue", "size" => "medium", "shape" => "sphere" ); extract($var_array, EXTR_PREFIX_SAME, "wddx"); echo " $color , $size , $shape , $wddx_size <br/>"; ?>
Run result:
blue, large, sphere, medium
Thanks for reading, I hope it can help you, thank you for your support of this site!