PHP extract() Function

In this chapter you will learn:

  1. Definition for PHP extract() Function
  2. Syntax for PHP extract() Function
  3. Parameter for PHP extract() Function
  4. Example - Convert array to variables
  5. Example - Use different extract mode

Definition

The extract() function converts elements in an array into variables.

Syntax

PHP extract() Function has the following syntax.

int extract ( array arr [, int options [, string prefix]] )

Parameter

ParameterIs RequiredDescription
arrayRequired.Array to use
extract_rulesOptional.The extract() function checks for invalid variable names and collisions with existing variable names. This parameter specifies how invalid and colliding names are treated.
prefixOptional.If EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS are used in the extract_rules parameter, a specified prefix is required.

Possible values for extract_rules:

ValueMeaning
EXTR_OVERWRITEDefault. On collision, overwrite the existing variable
EXTR_SKIP On collision, the existing variable is not overwritten
EXTR_PREFIX_SAMEOn collision, the variable name will be given a prefix
EXTR_PREFIX_ALL All variable names will be given a prefix
EXTR_PREFIX_INVALID Use the prefix specified by parameter three for illegal names
EXTR_IF_EXISTS Set variables only if they already exist
EXTR_PREFIX_IF_EXISTS Create prefixed variables only if non-prefixed version already exists
EXTR_REFS Extract variables as references rather than copies

Example 1

Convert array to variables


<?PHP/*from  j a  v  a  2 s . c o  m*/
$W = "W";
$capitalcities = array("E"=>"e", "A"=>"a", "W"=>"java2s.com");
extract($capitalcities);
print $W;
?>

After calling extract, the E, A, and W keys become variables ($E, $A, and $W), with their values set to e, a, and java2s.com, respectively.

By default, extract() will overwrite any existing variables.

Its second parameter tells 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 last option, EXTR_REFS, can be used on its own or in combination with others using the bitwise OR operator, |.

The code above generates the following result.

Example 2

Use different extract mode


<?Php//java 2  s.  c  o m
$capitalcities = array("E"=>"e", "A"=>"a", "W"=>"java2s.com");
$W = 'oldValue';
extract($capitalcities, EXTR_SKIP);
// leaves $W intact, as it exists already

print $W;
print $A;

extract($capitalcities, EXTR_PREFIX_SAME, "pre");
// creates variables $pre_W, $pre_A, etc

print $W;
print $pre_E;

extract($capitalcities, EXTR_PREFIX_ALL, "preAll");
extract($capitalcities, EXTR_PREFIX_ALL | EXTR_REFS, "pre");
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP implode() Function
  2. Syntax for PHP implode() Function
  3. Parameter for PHP implode() Function
  4. Example - Convert array to string
Home » PHP Tutorial » PHP Array Functions
PHP array() function
PHP array_change_key_case() function
PHP array_chunk() function
PHP array_combine() function
PHP array_count_values() function
PHP array_diff() function
PHP array_diff_assoc() Function
PHP array_diff_key() function
PHP array_diff_uassoc() function
PHP array_diff_ukey() function
PHP array_fill() function
PHP array_fill_keys() function
PHP array_filter() function
PHP array_flip() function
PHP array_intersect() Function
PHP array_intersect_assoc() Function
PHP array_intersect_key() function
PHP array_intersect_uassoc() Function
PHP array_intersect_ukey() Function
PHP array_key_exists() Function
PHP array_keys() function
PHP array_map() Function
PHP array_merge() function
PHP array_merge_recursive() Function
PHP array_multisort() Function
PHP array_pad() Function
PHP array_pop() function
PHP array_product() Function
PHP array_push() function
PHP array_rand() function
PHP array_reduce() Function
PHP array_replace() Function
PHP array_replace_recursive() Function
PHP array_reverse() Function
PHP array_search() Function
PHP array_shift() Function
PHP array_slice() Function
PHP array_splice() Function
PHP array_sum() Function
PHP array_udiff() Function
PHP array_udiff_assoc() Function
PHP array_udiff_uassoc() Function
PHP array_uintersect() Function
PHP array_uintersect_assoc() Function
PHP array_uintersect_uassoc() Function
PHP array_unique() Function
PHP array_unshift() Function
PHP array_values() Function
PHP array_walk() Function
PHP array_walk_recursive() Function
PHP arsort() Function
PHP asort() Function
PHP compact() Function
PHP count() Function
PHP current() Function
PHP each() Function
PHP end() Function
PHP explode() Function
PHP extract() Function
PHP implode() Function
PHP in_array() Function
PHP key() Function
PHP krsort() Function
PHP ksort() Function
PHP list() Function
PHP natcasesort() Function
PHP natsort() Function
PHP next() Function
PHP pos() Function
PHP prev() Function
PHP range() Function
PHP reset() Function
PHP rsort() Function
PHP shuffle() Function
PHP sizeof() Function
PHP sort() Function
PHP uasort() Function
PHP uksort() Function
PHP usort() Function