PHP extract function
The function extract of PHP Imports variables into the current symbol table from an associative array. This can be a bit tricky to understand, but you can think of it as doing the reverse of compact
.
You can check compact first if you are not familiar with this function https://blastcoding.com/en/php-compact-function/
Let’s take a look at the first example.
If my associative array looks like what’s shown in the table:
name | Pepe |
surname | Galeano |
age | 42 |
You will obtain the variables $name
, $surname
, and $age
with the values "Pepe"
, "Galeano"
, and 42
, respectively.
extract(array &$array, int $flags = EXTR_OVERWRITE, string $prefix = ""): int
extract
function on untrusted data, such as user-input data, for example, $_GET
, $_FILES
, $_POST
, and others.
Parametros
$array
– an asociative array
EXTR_PREFIX_ALL
or EXTR_PREFIX_INVALID
as flags.
$flags
– This parameter determines how non-valid/numeric keys and collisions are handled. EXTR_OVERWRITE is the default flag.
EXTR_OVERWRITE
– If there is a collision, it overwrites the existing variable in the current scope with the value from the associative array. For example, if $x = 101 and the associative array has a value “x” => 207, now $x will have the value 207.EXTR_SKIP
– If there is a variable that collides, it will not be overwritten.EXTR_PREFIX_SAME
– If there is a collision, it prefixes the variable name with the specified prefix in $prefix.EXTR_PREFIX_ALL
– Prefixes all variable names with the value given in $prefix.EXTR_PREFIX_INVALID
– Only prefixes non-valid/numeric variable names with the value of $prefix.EXTR_IF_EXISTS
– Overwrites the variable only if it already exists in the current symbol table; otherwise, it does nothing. This is useful for defining a list of valid variables and then extracting only those variables that were defined outside of $_REQUEST, for example.EXTR_PREFIX_IF_EXISTS
– Creates variable names with a prefix only if the unprefixed version of the same variable exists in the current symbol table.EXTR_REFS
– Extracts variables as references. This means that the values of the imported variables are still referenced by the values in the parameter array. You can use this flag on its own or combine it with any other flag using the OR operator in flags.
$prefix
– The value with which the variable will be prefixed if necessary. The prefix is separated from the key name by an underscore (_).
These are the flags for which you will need to use $prefix:
EXTR_PREFIX_SAME
EXTR_PREFIX_ALL
EXTR_PREFIX_INVALID
EXTR_PREFIX_IF_EXISTS
Return
It returns an integer that is the number of variables that were successfully imported.
Examples of PHP extract function
// Un arreglo asociativo con algunas variables $data = [ 'marca' => 'citroen', 'nombre'=>'c4', 'precio' => '19500', 'moneda' => 'euros', ]; // Usando extract para importar las variables extract($data); // Ahora puedes usar las variables como si se hubieran declarado individualmente echo "Marca:".$marca."<br/>"; echo "Nombre: $nombre<br/>"; echo "Precio: $precio<br/>"; echo "Moneda: $moneda<br/>";
Marca:citroen Nombre: c4 Precio: 19500 Moneda: euros