PHP extract Function | Array To Variable Conversion

PHP extract is an inbuilt function in PHP. It performs array to variable conversion. In other words, it converts array keys of an associative array to variable names, and the corresponding values to value of the variable. In this article, we will discuss the PHP extract Function. Also, we will discuss a few examples of using it.

Syntax

int extract($array, $rule, $prefix)

Paramaters

The extract function in PHP expects three parameters. However, only one parameter is mandatory and two are optional to pass. The description of the parameters is as follows:

  • $array: The first parameter to the function is a mandatory array. The function will operate on this input array.
  • $rule: The function checks for invalid variable names and collisions. The second parameter specified the rule to be used while dealing with invalid variable names and collisions. This parameter is optional and can take the following values:
    • EXTR_OVERWRITE: In case of collision, overwrite the existing variable.
    • EXTR_SKIP: In case of collision, don’t overwrite the existing variable.
    • EXTR_PREFIX_SAME: Prefix the variable name with $prefix if there is a collision.
    • EXTR_PREFIX_ALL: Prefix all variables with the $prefix parameter.
    • EXTR_PREFIX_INVALID: This rule tells the function to prefix invalid or numeric variable names with $prefix parameter.
    • EXTR_IF_EXISTS: Overwrite the variable if it already exists, otherwise do nothing.
    • EXTR_PREFIX_IF_EXISTS: Prefix the variable if it already exists, otherwise do nothing.
  • $prefix: The third parameter is also optional. It specified the prefix which is automatically separated by the array key with an underscore character. However, this parameter is mandatory if one of the following rules is set: EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS

Return Value

The PHP extract function returns an integer which is the count of total number of variables it could import from the array.

PHP extract function | array to variable conversion

Examples

Let’s discuss a few examples of using the PHP extract Function.

Example 1: Single-Dimensional Array

For instance, consider a single-dimensional array.

<?php

    $testArray = array(
        'apple' => 1,
        'banana' => 2,
        'cat' => 3
    );
    //The Array keys will be converted to variable names
    $numberOfVariables = extract($testArray);
    echo $numberOfVariables;
    //3 -- Total 3 variables extracted from the array

    var_dump($apple);
    //int(1) -- Value at key apple
    var_dump($banana);
    //int(2) -- Value at key banana
    var_dump($cat);
    //int(3) -- Value at key cat
?>

The array consists of three elements with different keys and value. Using the PHP extract function, we can extract variables as shown in the above example.

Example 2: Multi-Dimensional Array

Similarly, you can pass a multi-dimensional array in the PHP extract Function. However, it extracts variables only on the first level. Therefore, nested keys are not converted to variables but assigned as an array to the parent variable.

<?php

    $testArray = array(
        'apple' => 1,
        'banana' => array(
            'b1' => 10,
            'b2' => 20
        ),
        'cat' => 3
    );
    //The Array keys will be converted to variable names
    $numberOfVariables = extract($testArray);
    echo $numberOfVariables;
    //3 -- Total 3 variables extracted from the array

    print_r($banana); //$banana is assigned an array consisting of nested keys
    /*
    Array
    (
        [b1] => 10
        [b2] => 20
    )
    */
?>

Example 3: Using Prefix Rules

It might happen, that we already have an existing variable. In such a case, we can pass different rules in order to deal with collisions.

EXTR_PREFIX_SAME

EXTR_PREFIX_SAME rule states that if there is a collision, prefix the variable name with $prefix variable. In the below example, we pass ‘duplicate’ as the $prefix parameter.

<?php
    
    $banana = 'Original';

    $testArray = array(
        'apple' => 'A For Apple',
        'banana' => 'B For Banana',
        'cat' => 'C For Cat'
    );
    //The Array keys will be converted to variable names
    $numberOfVariables = extract($testArray, EXTR_PREFIX_SAME, 'duplicate');
    echo $numberOfVariables;
    //3 -- Total 3 variables extracted from the array

    print_r($banana);
    //Original -- The original value of the existing variable

    print_r($duplicate_banana);
    //B For Banana -- The prefix duplicate is prefixed in front of the existing variable

?>

EXTR_PREFIX_INVALID

Numeric variables are invalid in PHP. Using EXTR_PREFIX_INVALID, you can prefix a string in front of the invalid variable.

<?php
    
    $testArray = array(
        0 => 'A For Apple',
        1 => 'B For Banana',
        'valid' => 'C For Cat'
    );
    //The invalid variable (0, 1) will get the prefix. The valid variable will remain as it is
    $numberOfVariables = extract($testArray, EXTR_PREFIX_INVALID, 'invalid');

    print_r($invalid_0);
    //A For Apple -- Value at key 0

    print_r($invalid_1);
    //B For Banana --Value at key 1

    print_r($valid);
    //C For Cat -- Value at key valid

?>

Conclusion

In conclusion, we discussed the PHP extract Function. You can read more about it on PHP Official Documentation. Additionally, you can learn about more PHP Array Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *