PHP | extract() Function

The extract() Function is an inbuilt function in PHP. The extract() function does array to variable conversion. That is it converts array keys into variable names and array values into variable value. In other words, we can say that the extract() function imports variables from an array to the symbol table.

Syntax:

int extract($input_array, $extract_rule, $prefix)

Parameters: The extract() function accepts three parameters, out of which one is compulsory and other two are optional. All three parameters are described below:



  1. $input_array: This parameter is required. This specifies the array to use.
  2. $extract_rule: This parameter is optional. The extract() function checks for invalid variable names and collisions with existing variable names. This parameter specifies how invalid and colliding names will be treated. This parameter can take the following values:
    • EXTR_OVERWRITE: This rule tells that if there is a collision, overwrite the existing variable.
    • EXTR_SKIP: This rule tells that if there is a collision, don’t overwrite the existing variable.
    • EXTR_PREFIX_SAME: This rule tells that if there is a collision then prefix the variable name according to $prefix parameter.
    • EXTR_PREFIX_ALL: This rule tells that prefix all variable names accoring to $prefix parameter.
    • EXTR_PREFIX_INVALID: This rule tells that only prefix invalid/numeric variable names according to parameter $prefix.
    • EXTR_IF_EXISTS: This rule tells that to overwrite the variable only if it already exists in the current symbol table, otherwise do nothing.
    • EXTR_PREFIX_IF_EXISTS: This rule tells to create prefixed variable names only if the non-prefixed version of the same variable exists in the current symbol table.
  3. $prefix: This parameter is optional. This parameter specifies the prefix. The prefix is automatically separated from the array key by an underscore character. Also this parameter is required only when the parameter $extract_rule is set to EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS.

Return Value: The return value of extract() function is an integer and it represents the number of variables successfully extracted or imported from the array.

Examples:

Input : array("a" => "one", "b" => "two", "c" => "three")
Output :$a = "one" , $b = "two" , $c = "three"
Explanation: The keys in the input array will become the 
variable names and their values will be assigned to these
new variables.

Below programs illustrates working of extract() in PHP:

Example-1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
      
    // input array
    $state = array("AS"=>"ASSAM", "OR"=>"ORRISA", "KR"=>"KERELA");
      
    extract($state);
      
    // after using extract() function
    echo"\$AS is $AS\n\$KR is $KR\n\$OR is $OR";
      
?>

chevron_right


Output:

$AS is ASSAM
$KR is KERELA
$OR is ORRISA

Example-2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
      
    $AS="Original";
      
    $state = array("AS"=>"ASSAM", "OR"=>"ORRISA", "KR"=>"KERELA");
      
    // handling collisions with extract() function
    extract($state, EXTR_PREFIX_SAME, "dup");
      
    echo"\$AS is $AS\n\$KR is $KR\n\$OR if $OR \n\$dup_AS = $dup_AS";
      
?>

chevron_right


Output:

$AS is Original
$KR is KERELA
$OR is ORRISA 
$dup_AS = ASSAM

Reference:
http://php.net/manual/en/function.extract.php



My Personal Notes arrow_drop_up

Sudipta sahana BTech 2nd year Cse student

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.