只是想知道是否有办法(或者甚至可能).
所以我有一段与此类似的代码.我有一个字符串,其值包含方括号,类似于访问数组键时使用的字符串.我想通过使用字符串值来创建该数组键.我希望这段代码能够更清楚地表达我的意思.
// String that has a value similar to an array key
$string = 'welcome["hello"]';
$$string = 'thisworks';
// I could then print the array keys value like this
print( $welcome["hello"] );
// This would hopefully output 'thisworks'.
Run Code Online (Sandbox Code Playgroud)
但是,似乎无法让它正常工作.是否有可能(或者我还能怎么做)?
是否可以创建指向数组或嵌套对象的变量变量?php文档明确指出你不能指向SuperGlobals,但它不清楚(至少对我来说)这是否适用于一般的数组.
这是我对数组var var的尝试.
// Array Example
$arrayTest = array('value0', 'value1');
${arrayVarTest} = 'arrayTest[1]';
// This returns the correct 'value1'
echo $arrayTest[1];
// This returns null
echo ${$arrayVarTest};
Run Code Online (Sandbox Code Playgroud)
这是一些简单的代码,用于显示对象var var的含义.
${OBJVarVar} = 'classObj->obj';
// This should return the values of $classObj->obj but it will return null
var_dump(${$OBJVarVar});
Run Code Online (Sandbox Code Playgroud)
我错过了一些明显的东西吗?