Course recommendation:

PHP extract() function

Example

<?php
$a = "Original";
Assign the values "Cat", "Dog", and "Horse" to the variables $a, $b, and $c:
$my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse");;
extract($my_array)
?>

Laufende Beispiele

echo "\$a = $a; \$b = $b; \$c = $c";

Definition and Usage

The extract() function imports variables from the array into the current symbol table.

This function uses array keys as variable names and array values as variable values. For each element in the array, it creates a corresponding variable in the current symbol table. second parameter type

Used to specify how the extract() function should handle conflicts when a variable already exists and there is a same-named element in the array.

This function returns the number of variables successfully imported into the symbol table.

syntaxdescriptionextract(extract_rulesextract(prefix,
) parameter
description array
extract_rules

required. Specifies the array to be used.

Possible values:

  • EXTR_OVERWRITE - Default. Overwrites existing variables if there is a conflict
  • EXTR_SKIP - Does not overwrite existing variables if there is a conflict
  • EXTR_PREFIX_SAME - Adds a prefix to variable names if there is a conflict prefix.
  • EXTR_PREFIX_ALL - Adds a prefix to all variable names prefix.
  • EXTR_PREFIX_INVALID - Adds a prefix only before invalid or numeric variable names prefix.
  • EXTR_IF_EXISTS - Overwrites the values only if a variable with the same name already exists in the current symbol table. The others are not processed.
  • EXTR_PREFIX_IF_EXISTS - Creates a prefixed variable name only if a variable with the same name already exists in the current symbol table. The others are not processed.
  • EXTR_REFS - Extracts variables as references. The imported variables still refer to the values of the array parameters.
prefix

optional. Please note prefix Nur extract_type wenn der Wert EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID oder EXTR_PREFIX_IF_EXISTS ist, wird dies benötigt. Wenn das Ergebnis nach dem Hinzufügen des Präfixes kein gültiger Variablenname ist, wird es nicht in die Symboltabelle importiert.

Automatisch ein Unterstrich zwischen Präfix und Array-Schlüsselname hinzufügen.

Technische Details

Rückgabewert: Gibt die Anzahl der erfolgreich importierten Variablen in die Symboltabelle zurück.
PHP-Version: 4+
Aktualisierungsprotokoll:

extract_rules Werte EXTR_REFS sind in PHP 4.3 hinzugefügt worden.

extract_rules Werte EXTR_IF_EXISTS und EXTR_PREFIX_IF_EXISTS sind in PHP 4.2 hinzugefügt worden.

Ab PHP 4.0.5 gibt diese Funktion die Anzahl der erfolgreich importierten Variablen in die Symboltabelle zurück.

extract_rules Werte EXTR_PREFIX_INVALID sind in PHP 4.0.5 hinzugefügt worden.

Ab PHP 4.0.5extract_rules Werte EXTR_PREFIX_ALL enthalten auch numerische Variablen.

Mehr Beispiele

Beispiel 1

Verwenden Sie alle Parameter:

<?php
$a = "Original";
$my_array = array("a" => "Cat", "b" => "Dog", "c" => "Horse");
extract($my_array, EXTR_PREFIX_SAME, "dup");
echo "\$a = $a; \$b = $b; \$c = $c; \$dup_a = $dup_a";
?>

Laufende Beispiele