NOVINKA: Najžiadanejšie rekvalifikačné kurzy teraz s 50% zľavou + kurz AI ZADARMO. Nečakaj, táto ponuka dlho nevydrží! Zisti viac:

PHP - extract

Trieda

Koreň \ Bez triedy

Metóda - extract

(PHP 4, PHP 5, PHP 7)

It extracts variables from an array into the current scope. It uses array keys as variable names, the function checks if variable names are valid and also if the variable doesn't already exist.

It's a potentially dangerous function and we should consider what data can get into the extracted array and if a user can access the array.

Procedurálne

  • function extract (array &$array, int $flags = EXTR_OVERWRITE, string $prefix = NULL) : int

Parametre

NázovDátový typPredvolená hodnotaPopis
&$arrayarray

The input array.

$flagsint EXTR_OVERWRITE

Specifies how collisions and invalid values are treated.

  • EXTR_OVERWRITE- Overwrites existing variables.
  • EXTR_SKIP - Does not overwrite existing variables.
  • EXTR_PREFIX_SAME - If there is a collision, adds a prefix to the variable name.
  • EXTR_PREFIX_ALL - Adds a prefix to all variable names.
  • EXTR_PREFIX_IN­VALID - Adds a prefix only to invalid variable names (e.g. numeric names).
  • EXTR_IF_EXISTS - Overwrites a variable only if it exists. Otherwise, the variable will be not created. By creating allowed variables, the import of the right elements can be treated e.g. from POST.
  • EXTR_PREFIX_IF_E­XISTS - Adds a prefix to the variable name only if the variable exists. The non-existing variables will be not extracted.
  • EXTR_REFS - Extracts variables as references to original elements of the array. This flag can be used with others.
$prefixstring NULL

The third optional parameter is a prefix of the variable name. It's required with following flags: EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_IN­VALID or EXTR_PREFIX_IF_E­XISTS. The underscore character("_") is automatically added after the prefix.

Mávratovej hodnoty

Vracia: int

A number of successfully extracted variables.

Príklady

<?php
$array = ['a' => 1, 'b' => '2', 'c' => 3.0];

extract($array);

echo $a;

The extract() function can be used very well when creating templates with own PHP structures.

Súvisiace manuály

        Aktivity