PHP Array Extract

We have seen about PHP string extract few days before, to get particular portion of given input string using PHP in built functions, like, substr(), mb_substr() and etc. And now, we are going to see another PHP function to extract given associative array.

This function is named as extract(), that is, for extracting each of the key and value of an array, to create PHP variable with the name of array key , which will be associated with appropriate array value.

PHP extract()

This function accepts three arguments as listed below.

  • $input_array – Associative array from which the keys are converted into PHP variables.
  • $extract_type – Based on this type, the behavior of array extraction will be varied during some operations like, overwriting existing variables, creating prefixed variables and etc. PHP includes several predefined constants to be used as the value of this argument.
  • $variable_prefix – This argument will have string input which is to be prefixed on creating PHP variable, to store input array elements.

Among the above list of arguments of PHP extract() function, $input_array is only the mandatory argument, whereas, the rest of them are optional that could be used on need basis.

And, behavior of this function with respect to add prefix for PHP variables, will be varied depends on the value of $extract_type parameter. So, we need to know all the available list of PHP constants used for specifying extract type.

php_array_extract

PHP Constants for Extract Type

  • EXTR_OVERWRITE – This is the default value used for this PHP function, if no such argument is sent while invoking this method. By using this constant, if any variable exists already with the same name of the array key, then it will be overwrite.
  • EXTR_SKIP – Unlike EXTR_OVERWRITE, PHP extract() will skip extracting particular element, if there is any existing variable with its key name.
  • EXTR_IF_EXISTS – Using this constant, this function will extract array element, if and only if, the variables with the name of the array key is existing in the symbol table. For example,
    <?php
    $partA = "";
    $partB = "";
    $input_array = array("partA" => "Stories","partB" => "Rhymes", "partC" => "Concepts");
    extract($input_array,EXTR_IF_EXISTS);
    print '$partA='. $partA . '<br/>';
    print '$partB='. $partB . '<br/>';
    print '$partC='. $partC . '<br/>';
    ?>
    

    Note: As we have defined $partA and $partB before extract, then, this function will extract and overwrite these variables stored in current symbol table. And then, $partC is not defined previously, there is no such variable in symbol table, and hence, extract() will omit the element containing undefined variable names. And return PHP notice error as follows.

    $partA=Stories
    $partB=Rhymes
    
    Notice: Undefined variable: partC in D:\php_env\htdocs\php_samples\php_extract.php on line 8
    $partC=
    
  • EXTR_REFS – With this parameter, the variables are created as references of corresponding input array element. So, we can change the value of the array element by using the newly created PHP variables on extract. For example,
    <?php
    $input_array = array("partA" => "Stories","partB" => "Rhymes", "partC" => "Concepts");
    print "<strong>Before Extract:</strong><br/>";
    print_r($input_array);
    extract($input_array,EXTR_REFS);
    $partA = "Alphabets";
    print "<br/><br/><strong>After Extract:</strong><br/>";
    print_r($input_array);
    ?>
    

    Note: $input_array["partA"] is changed by using $partA variable reference, that we can see with the following output of this program.

    Before Extract:
    Array ( [partA] => Stories [partB] => Rhymes [partC] => Concepts ) 
    
    After Extract:
    Array ( [partA] => Alphabets [partB] => Rhymes [partC] => Concepts )
    
  • Other Extract Type Constants to Create Prefixed Variables

    Added to the above list of PHP extract type constants, some more constants are there in use. These are the flag to add third argument $variable_prefix at the beginning of each variable created on extract, separated with (_) underscore symbol.

    • EXTR_PREFIX_SAME – Extracted variables will be prefixed if there is any variable defined already with same name.
    • EXTR_PREFIX_ALL – All extracted variables will be prefixed, unconditionally.
    • EXTR_PREFIX_INVALID – If the extracted key from he given input array is not valid for being a variable name, then, it will be prefixed with the given string.
    • EXTR_PREFIX_IF_EXISTS – This constant will work as similar as EXTR_IF_EXISTS, except, it will create new prefixed variable instead of overwriting existing symbol table entry.

    Caution

    It is not good programming practice to use this extract() function with PHP global array, Why because,

    • If we use the extracted variable into our PHP program, it will cause poor maintainability issue, meaning that, while feature enhancement, it would be tough to understand the variable references.
    • And, this will cause security issue on extracting global data, like, $_GET, $_REQUEST and etc, without having knowledge of whether these are genuine or not.

    This PHP tutorial was added on July 30, 2013.

    «

    »

    ↑ Back to Top