1. 程式人生 > 其它 >從陣列中將變數匯入到當前的符號表

從陣列中將變數匯入到當前的符號表

1)

小結

類比golang build 時的版本引數

extract

(PHP 4, PHP 5, PHP 7, PHP 8)

extract—Import variables into the current symbol table from an array

Description

extract(array&$array,int$flags=EXTR_OVERWRITE,string$prefix= ""):int

Import variables from an array into the current symbol table.

Checks each key to see whether it has a valid variable name. It also checks for collisions with existing variables in the symbol table.

Warning

Do not useextract()on untrusted data, like user input (e.g.$_GET,$_FILES).

Parameters

array

An associative array. This function treats keys as variable names and values as variable values. For each key/value pair it will create a variable in the current symbol table, subject toflagsandprefix

parameters.

You must use an associative array; a numerically indexed array will not produce results unless you useEXTR_PREFIX_ALLorEXTR_PREFIX_INVALID.

flags

The way invalid/numeric keys and collisions are treated is determined by the extractionflags. It can be one of the following values:

EXTR_OVERWRITE
If there is a collision, overwrite the existing variable.
EXTR_SKIP
If there is a collision, don't overwrite the existing variable.
EXTR_PREFIX_SAME
If there is a collision, prefix the variable name withprefix.
EXTR_PREFIX_ALL
Prefix all variable names withprefix.
EXTR_PREFIX_INVALID
Only prefix invalid/numeric variable names withprefix.
EXTR_IF_EXISTS
Only overwrite the variable if it already exists in the current symbol table, otherwise do nothing. This is useful for defining a list of valid variables and then extracting only those variables you have defined out of$_REQUEST, for example.
EXTR_PREFIX_IF_EXISTS
Only create prefixed variable names if the non-prefixed version of the same variable exists in the current symbol table.
EXTR_REFS
Extracts variables as references. This effectively means that the values of the imported variables are still referencing the values of thearrayparameter. You can use this flag on its own or combine it with any other flag by OR'ing theflags.

Ifflagsis not specified, it is assumed to beEXTR_OVERWRITE.

prefix

Note thatprefixis only required ifflagsisEXTR_PREFIX_SAME,EXTR_PREFIX_ALL,EXTR_PREFIX_INVALIDorEXTR_PREFIX_IF_EXISTS. If the prefixed result is not a valid variable name, it is not imported into the symbol table. Prefixes are automatically separated from the array key by an underscore character.

Return Values

Returns the number of variables successfully imported into the symbol table.

Examples

Example #1extract()example

A possible use forextract()is to import into the symbol table variables contained in an associative array returned bywddx_deserialize().

<?php

/*Supposethat$var_arrayisanarrayreturnedfrom
wddx_deserialize*/

$size="large";
$var_array=array("color"=>"blue",
"size"=>"medium",
"shape"=>"sphere");
extract($var_array,EXTR_PREFIX_SAME,"wddx");

echo"$color,$size,$shape,$wddx_size\n";

?>

The above example will output:

blue, large, sphere, medium

The$sizewasn't overwritten because we specifiedEXTR_PREFIX_SAME, which resulted in$wddx_sizebeing created. IfEXTR_SKIPwas specified, then$wddx_sizewouldn't even have been created.EXTR_OVERWRITEwould have caused$sizeto have value "medium", andEXTR_PREFIX_ALLwould result in new variables being named$wddx_color,$wddx_size, and$wddx_shape.

Notes

Warning

Do not useextract()on untrusted data, like user input (i.e.$_GET,$_FILES, etc.). If you do, make sure you use one of the non-overwritingflagsvalues such asEXTR_SKIPand be aware that you should extract in the same order that's defined invariables_orderwithin thephp.ini.

See Also

  • compact()- Create array containing variables and their values
  • list()- Assign variables as if they were an array

PHP: extract - Manual https://www.php.net/manual/zh/function.extract.php

<?php

/*假定$var_array是wddx_deserialize返回的陣列*/

$size="large";
$var_array=array("color"=>"blue",
"size"=>"medium",
"shape"=>"sphere");
extract($var_array,EXTR_PREFIX_SAME,"wddx");

echo"$color,$size,$shape,$wddx_size\n";

?>

以上例程會輸出:

blue, large, sphere, medium

extract

(PHP 4, PHP 5, PHP 7, PHP 8)

extract—從陣列中將變數匯入到當前的符號表

說明

extract(array&$array,int$flags=EXTR_OVERWRITE,string$prefix= ""):int

本函式用來將變數從陣列中匯入到當前的符號表中。

檢查每個鍵名看是否可以作為一個合法的變數名,同時也檢查和符號表中已有的變數名的衝突。

警告

不要對不可信的資料使用extract(),類似使用者輸入 (例如$_GET$_FILES)。

引數

array

一個關聯陣列。此函式會將鍵名當作變數名,值作為變數的值。 對每個鍵/值對都會在當前的符號表中建立變數,並受到flagsprefix引數的影響。

必須使用關聯陣列,數字索引的陣列將不會產生結果,除非用了EXTR_PREFIX_ALL或者EXTR_PREFIX_INVALID

flags

對待非法/數字和衝突的鍵名的方法將根據取出標記flags引數決定。可以是以下值之一:

EXTR_OVERWRITE
如果有衝突,覆蓋已有的變數。
EXTR_SKIP
如果有衝突,不覆蓋已有的變數。
EXTR_PREFIX_SAME
如果有衝突,在變數名前加上字首prefix
EXTR_PREFIX_ALL
給所有變數名加上字首prefix
EXTR_PREFIX_INVALID
僅在非法/數字的變數名前加上字首prefix
EXTR_IF_EXISTS
僅在當前符號表中已有同名變數時,覆蓋它們的值。其它的都不處理。 舉個例子,以下情況非常有用:定義一些有效變數,然後從$_REQUEST中僅匯入這些已定義的變數。
EXTR_PREFIX_IF_EXISTS
僅在當前符號表中已有同名變數時,建立附加了字首的變數名,其它的都不處理。
EXTR_REFS
將變數作為引用提取。這有力地表明瞭匯入的變數仍然引用了array引數的值。可以單獨使用這個標誌或者在flags中用 OR 與其它任何標誌結合使用。

如果沒有指定flags,則被假定為EXTR_OVERWRITE

prefix

注意prefix僅在flags的值是EXTR_PREFIX_SAMEEXTR_PREFIX_ALLEXTR_PREFIX_INVALIDEXTR_PREFIX_IF_EXISTS時需要。 如果附加了字首後的結果不是合法的變數名,將不會匯入到符號表中。字首和陣列鍵名之間會自動加上一個下劃線。

返回值

返回成功匯入到符號表中的變數數目。

範例

示例 #1extract()例子

extract()的一種可能用法是將wddx_deserialize()返回的結合陣列中的內容匯入到符號表變數中去。

<?php

/*假定$var_array是wddx_deserialize返回的陣列*/

$size="large";
$var_array=array("color"=>"blue",
"size"=>"medium",
"shape"=>"sphere");
extract($var_array,EXTR_PREFIX_SAME,"wddx");

echo"$color,$size,$shape,$wddx_size\n";

?>

以上例程會輸出:

blue, large, sphere, medium

$size沒有被覆蓋,因為指定了EXTR_PREFIX_SAME,這使得$wddx_size被建立。如果指定了EXTR_SKIP,則$wddx_size也不會被建立。EXTR_OVERWRITE將使$size的值為“medium”,EXTR_PREFIX_ALL將建立新變數$wddx_color$wddx_size$wddx_shape

註釋

警告

不要對不能信任的資料使用extract(),例如使用者的輸入($_GET$_FILES...)。 如果這樣做,要確保使用不會覆蓋的flags值,例如EXTR_SKIP,並且要留意應該按照variables_orderphp.ini裡 定義的順序來提取。