Hudzilla.org - the homepage of Paul Hudson
Contents > Arrays > Array-specific functions Wish List | Report Bug | About Me ]

5.6.4     Converting an array to individual variables: extract()

This is NOT the latest copy of this book; click here for the latest version.

int extract ( array source [, int extract_type [, string prefix]])

Extract() is a very popular function that converts elements in an array into variables in their own right. Extract takes a minimum of one parameter, an array, and returns the number of elements extracted. This is best explained using code, so here goes:

<?php
    $Wales
= 'Swansea';
    
$capitalcities['England'] = 'London';
    
$capitalcities['Scotland'] = 'Edinburgh';
    
$capitalcities['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".

This behaviour 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. Here are the possible values of the second parameter:

EXTR_OVERWRITE

On collision, overwrite the existing variable

EXTR_SKIP

On collision, do not overwrite the existing variable

EXTR_PREFIX_SAME

On collision, prefix the variable name with the prefix specified by parameter three

EXTR_PREFIX_ALL

Prefix all variables with the prefix specified by parameter three, whether or not there is a collision

EXTR_PREFIX_INVALID

Only use the prefix specified by parameter three when variables names would otherwise be illegal (e.g. "$9")

EXTR_IF_EXISTS

Only set variables if they already exist

EXTR_PREFIX_IF_EXISTS

Only create prefixed variables if non-prefixed version already exists

EXTR_REFS

Extract variables as references

The last option, EXTR_REFS, can either be used on its own or in combination with others using the bitwise OR operator |.

Here are some examples based upon the $capitalcities array from the previous example:

<?php
    $Wales
= 'Swansea';
    
extract($capitalcities, EXTR_SKIP);
    print
$Wales;
    print
$Scotland;
    
extract($capitalcities, EXTR_PREFIX_SAME, "country");
    print
$Wales;
    print
$country_England;
    
extract($capitalcities, EXTR_PREFIX_ALL, "country");
?>

On line one we pre-set $Wales to be a value so that you can clearly see how the second parameter works. On line two we call extract() using two parameters, with EXTR_SKIP as parameter two so that our $Wales will not be overwritten. However, as you will be able to see if you run the script, $England and $Scotland were set.

On line five we use EXTR_PREFIX_SAME, which will extract variables and use the third parameter as a prefix if it finds any collisions. As we set $Wales ourselves and our previous call to extract() created $England and $Scotland, all our variables will need to be prefixed. As you can see on line six, $Wales is still set to "Swansea", and on line seven we have our prefixed variable $country_England. Note that PHP places an underscore _ after your prefix to make the variable easy to read.

Finally we call extract() with EXTR_PREFIX_ALL, which will unconditionally create variables with prefixes, overwriting $country_England, etc. Note that EXTR_OVERWRITE is rarely if ever used, because it is the same as using extract() without second or third parameters.





<< 5.6.3 Filtering your array through a function: array_filter()   5.6.5 Checking whether an element exists: in_array() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
nageshajab@yahoo.com - 30 Aug 2008

Sir,
I am a new programmer to php. I am from Pune, India. I have learnt previously pascal, cobol, cpp, Ad java, .net framework.

Currently I am working in 'Jopasana Software and Systems' on the .net platform (Asp.net and Ms Sql Server). My company told me to learn PHP, mono and MySql for our future work.

so I have searched over the internet and found your book, This book is very very good, it gives me a very good understanding of PHP.

If you have any good more links for mysql and mono, please tell me.
Yours faithfully
NBA

A PHP User - 30 Aug 2008

extract() is often used in processing forms that have been submitted from a webpage:

extract($_POST);

Doing this gives you a variable for each field submitted by the form, which can be nice for further processing.

A PHP User - 30 Aug 2008

Why is extract() a popular function? What's the senerio used to do most the time?



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is five plus one?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow