Converting an array to individual variables

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.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Checking whether an element exists >>

Previous chapter: Filtering your array through a function

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.