2017-04-14 38 views
1

我分配與$變量數組(有不同的名稱)有一定價值,如:

array($one, $two, $three); 

現在我是一個搜索的方式,有什麼辦法可以指定這個上面鍵名稱相同的數組變量名直接,最後我想使這個陣列,如:

array(
     "one" => $one, 
     "two" => $two, 
     "three" => $three 
) 

如果能可能在與可變分配陣列的時間的話,我會爲我好,然後我可以很容易地提取()這個數組我不必做操作來打開這個數組,並賦值這些值$變量的名稱,如果有任何函數( ),目前這則這將是很好,請若有人知道

回答

0

compactextract逆幫助。使用這樣的:

$array = compact ('one', 'two', 'three'); 

這將尋找名爲「一」的變量,「兩節」,「三化」,並做你正在尋找什麼。

+0

這就是我在尋找的感謝@Lorenz Meyer –

0

這裏有一個限制,你必須添加2次get_defined_vars();

PHP code demo

<?php 

$someVariable=1; 
$someVariable=2; 
$someVariable=3; 
$definedOld=get_defined_vars();//this will bring all variables defined till now. 

$one="test1"; 
$two="test2"; 
$three="test3"; 

$definedNew = get_defined_vars();//this will bring all variables defined till now. 

$variables=array_diff($definedNew,$definedOld);//finding the difference 
print_r($variables); 
+0

我知道這可以通過一些邏輯來完成,但我正在尋找一個函數 –

相關問題