Name
extract()
Synopsis
int extract ( arrayarr[, intoptions[,string prefix]] )
The extract() function converts elements in an array into variables in their own right, an act commonly called "exporting" in other languages. Extract takes a minimum of one parameter, an array, and returns the number of elements extracted. This is best explained using code:
$Wales = "Swansea";
$capitalcities = array("England"=>"London",
"Scotland"=>"Edinburgh", "Wales"=>"Cardiff");
extract($capitalcities);
print $Wales;After calling extract, the England, Scotland, and Wales keys become variables in their own right ($England, $Scotland, and $Wales), with their values set to London, Edinburgh, and Cardiff, respectively. By default, extract() will overwrite any existing variables, meaning that $Wales's original value of Swansea will be overwritten with Cardiff. The new variables are copies of those in the array, and not references.
This behavior can be altered using the second parameter, and averted using the third parameter. Parameter two takes a special constant value that allows you to decide how values will be treated if there is an existing variable, and parameter three allows you to prefix each extract variable with a special string. The possible values of the second parameter are shown in Table 5-6.
Table 5-6. Possible values for the second parameter to extract()
|
EXTR_OVERWRITE |
On collision, overwrite the existing variable |
|
EXTR_SKIP |
On collision, do not overwrite the existing variable |
|
EXTR_PREFIX_SAME ... |