簡體   English   中英

訪問全局變量變量的問題

[英]Issue accessing a global variable variable

我無法從函數中訪問全局變量變量(作為關聯數組):

$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"
}

給出通知:未定義的變量和注意:未定義的索引

$GLOBALS[${'arr'.$x}]轉換為$GLOBALS[$arr1]$arr1在函數范圍內不存在,加上它是一個數組。 您需要獲取一個arr1字符串作為索引:

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

這轉換為$GLOBALS['arr1']

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM