Php extract
From w3cyberlearnings
Contents |
PHP function extract
This function extracts from array and imports into the current symbol table.
Syntax extract
- array: array input
- extramode:
- EXTR_OVERWRITE- Default, when there is a collision, overwrite the existing variable.
- ExTR_SKIP - when there is a collision, don't override the existing variable.
- EXTR_PREFIX_SAME - where there is a collision, prefix the variable with prefix_string. (Example, prefix_string_variable)
- EXTR_PREFIX_ALL - Prefix all variables with prefix_string.
- EXTR_PREFIX_INVALID - Numeric or invalid variable name will be given a prefix_string.
- EXTR_PREFIX_IF_EXISTS - Only add prefix to variables if the same variable exists in the current symbol table
- EXTR_REFS - Extracts variables as references. The imported variables are still referencing the values of the array parameter
- EXTR_IF_EXISTS - overwrite the variable if it already exists in the current symbol table, otherwise do nothing.
extract(array, extramode, prefix_string);
Example 1
<?php $records = array('Alyssa' => 10, 'Emma' => 3, 'Landon' => 5); extract($records); echo "\$Alyssa=" . $Alyssa . "<br/>"; echo "\$Emma=" . $Emma . "<br/>"; echo "\$Landon=" . $Landon . "<br/>"; ?>
Output
$Alyssa=10 $Emma=3 $Landon=5
Example 2
<?php $john = 83; // john variable $person = array( 'christ' => 84, 'john' => 31, // john in array 'marry' => 73 ); //abcd is prefix, and thus john existed // thus $abcd_john is for join extract($person, EXTR_PREFIX_SAME, 'abcd'); echo "\$christ=$christ.<br/>"; echo "\$john=$john.<br/>"; echo "\$marry=$marry.<br/>"; echo "\$abcd_john=$abcd_john.<br/>"; ?>
Output
$christ=84. $john=83. $marry=73. $abcd_john=31.
Example 3
<?php $john = 83; // john variable $person = array( 'christ' => 84, 'john' => 31, // john in array 'marry' => 73 ); //abcd is prefix, and thus john existed // thus $abcd_john is for join extract($person, EXTR_SKIP, 'abcd'); echo "\$christ=$christ.<br/>"; echo "\$john=$john.<br/>"; echo "\$marry=$marry.<br/>"; ?>
Output
$christ=84. $john=83. $marry=73.
Related Links
- array_change_key_case
- array_chunk
- array_combine
- array_count_values
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_diff
- array_fill_keys
- array_fill
- array_filter
- array_flip
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_intersect
- array_key_ exists
- array_keys