PHP -Extract()- Funktion
Beispiel
Weisen Sie den Variablen $a, $b und $c die Werte "Katze", "Hund" und "Pferd" zu:
<?php
$a = "Original";
$my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse");
extract($my_array);
echo "\$a = $a; \$b = $b; \$c = $c";
?>
Definition und Verwendung
Die Funktion extract() importiert Variablen aus einem Array in die lokale Symboltabelle.
Diese Funktion verwendet Array-Schlüssel als Variablennamen und Werte als Variablenwerte. Für jedes Element wird eine Variable in der aktuellen Symboltabelle erstellt.
Diese Funktion gibt die Anzahl der bei Erfolg extrahierten Variablen zurück.
Syntax
extract(array, extract_rules, prefix)
Parameterwerte
Parameter | Description |
---|---|
array | Required. Specifies the array to use |
extract_rules | Optional. The extract() function checks for invalid variable names and collisions with existing variable names. This parameter specifies how invalid and colliding names are treated. Possible values:
|
prefix | Optional. If EXTR_PREFIX_SAME, EXTR_PREFIX_ALL, EXTR_PREFIX_INVALID or EXTR_PREFIX_IF_EXISTS are used in the extract_rules parameter, a specified prefix is required.
This parameter specifies the prefix. The prefix is automatically separated from the array key by an underscore character. |
Technische Details
Rückgabewert: | Gibt die Anzahl der bei Erfolg extrahierten Variablen zurück |
---|---|
PHP-Version: | 4+ |
PHP-Änderungsprotokoll: | Der Extract_rules- Wert EXTR_REFS wurde in PHP 4.3 hinzugefügt. Die Extract_rules- Werte EXTR_IF_EXISTS und EXTR_PREFIX_IF_EXISTS wurden in PHP 4.2 hinzugefügt. Ab PHP 4.0.5 gibt diese Funktion nun die Anzahl der extrahierten Variablen zurück. Der Extract_rules- Wert EXTR_PREFIX_INVALID wurde in PHP 4.0.5 hinzugefügt. Ab PHP 4.0.5 enthält der Extract_rules- Wert EXTR_PREFIX_ALL jetzt auch numerische Variablen. |
Mehr Beispiele
Beispiel
Verwendung aller 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";
?>
❮ PHP-Array-Referenz