2017-01-23 31 views
1

我正在從具有在函數內無法訪問全局變量變量(即關聯數組)的全局變量變量:發行訪問

$arr1 = array(
    1 => "Alex1", 
    2 => "Blah", 
    3 => "Charlie" 
); 

$arr2 = array(
    1 => "D", 
    2 => "E", 
    3 => "F" 
); 

function GetVal() 
{ 
    $x = 1; // But could be any value 
    $dd = $GLOBALS[${'arr'.$x}]; 

$ouput = $dd[1]; // should be "Alex1" 
} 

發出通知:未定義變量和注意:未定義指數

回答

0

$GLOBALS[${'arr'.$x}]轉換爲$GLOBALS[$arr1]$arr1不存在於函數作用域中,再加上它是一個數組。你需要得到的arr1字符串作爲索引使用:

$dd = $GLOBALS['arr'.$x]; // or use "arr$x" 

這相當於$GLOBALS['arr1']

+0

非常感謝:) – AliC