PHP extract() Functie
Voorbeeld
Ken de waarden "Kat", "Hond" en "Paard" toe aan de variabelen $a, $b en $c:
<?php
$a = "Original";
$my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse");
extract($my_array);
echo "\$a = $a; \$b = $b; \$c = $c";
?>
Definitie en gebruik
De functie extract() importeert variabelen in de lokale symbooltabel vanuit een array.
Deze functie gebruikt array-sleutels als variabelenamen en waarden als variabelewaarden. Voor elk element wordt een variabele gemaakt in de huidige symbolentabel.
Deze functie retourneert het aantal variabelen dat bij succes is geëxtraheerd.
Syntaxis
extract(array, extract_rules, prefix)
Parameterwaarden
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
Winstwaarde: | Retourneert het aantal variabelen dat bij succes is geëxtraheerd |
---|---|
PHP-versie: | 4+ |
PHP-wijzigingslogboek: | De extract_rules- waarde EXTR_REFS is toegevoegd in PHP 4.3. De extract_rules- waarden EXTR_IF_EXISTS en EXTR_PREFIX_IF_EXISTS zijn toegevoegd in PHP 4.2. Vanaf PHP 4.0.5 retourneert deze functie nu het aantal geëxtraheerde variabelen. De extract_rules- waarde EXTR_PREFIX_INVALID is toegevoegd in PHP 4.0.5. Vanaf PHP 4.0.5 bevat de extract_rules- waarde EXTR_PREFIX_ALL nu ook numerieke variabelen. |
Meer voorbeelden
Voorbeeld
Alle parameters gebruiken:
<?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";
?>
❮ Referentie voor PHP-array