![](/img/trans.png)
[英]In PHP, how can I get the variable name that passed in in a function call?
[英]How can I get the name of a variable?
我有幾個變數。 一個變量包含一個字符串,一個變量包含一個對象,一個變量包含一個數組。
$mystring = "hello im input";
$myobject = new userclass;
$myarray = array ( 0 => 'zero', 1 => "one");
我想創建一個包含變量名作為鍵和值作為值的數組,因此可以將該數組發送給函數。
預期結果將是:
$multi_array = array (
'mystring' => "hello im input",
'myobject' => new userclass,
'myarray' => array ( 0 => 'zero', 1 => "one")
)
我使用鍵來獲取數組的名稱,並使用getclass()
來獲取對象的名稱。 但是,如何獲取變量的名稱?
function i_do_extract($multi_array) {
extract($multi_array);
// Do Rest
}
我不知道這是否對您有幫助,但是您可以使用:
http://www.php.net/manual/zh/function.get-defined-vars.php
列出定義的變量。 請記住,PHP確實支持諸如
$str = "test";
$test = "hmmm";
echo $$str;
這將輸出hmmm
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.