Php 为什么我会得到;未定义变量";当尝试解析多维数组时?
我试图通过描述数组路径的字符串从多维数组中检索一个项(如Php 为什么我会得到;未定义变量";当尝试解析多维数组时?,php,Php,我试图通过描述数组路径的字符串从多维数组中检索一个项(如first.second.third) 我选择了如下所示的方法(): 那么,为什么不能正确计算呢?我认为$符号只接受变量名(即变量名)。实际上,您正在尝试读取名为“$GLOBALS['a']['b']['c']”的变量,该变量不存在 另外($GET_VARIABLE似乎是您的输入字符串),您可以尝试以下操作: $path = explode('.', $GET_VARIABLE); echo $GLOBALS[$path[0]][$path
first.second.third
)
我选择了如下所示的方法():
那么,为什么不能正确计算呢?我认为
$
符号只接受变量名(即变量名)。实际上,您正在尝试读取名为“$GLOBALS['a']['b']['c']
”的变量,该变量不存在
另外($GET_VARIABLE
似乎是您的输入字符串),您可以尝试以下操作:
$path = explode('.', $GET_VARIABLE);
echo $GLOBALS[$path[0]][$path[1]][path[2]];
将其包装在一个合适的循环中,使其更具动态性;这看起来很简单。看起来PHP将整个字符串视为变量名,而不是数组 您可以尝试使用以下方法,因为这可能更简单
<?php
// The path into the array
$GET_VARIABLE = "a.b.c";
// Some example data
$GLOBALS["a"]= array("b"=>array("c"=>"foo"));
// Construct an accessor into the array
$variablePath = explode( ".", $GET_VARIABLE );
//$accessor = implode( "' ][ '", $variablePath );
//$variable = "\$GLOBALS[ '". $accessor . "' ]";
// Print the value for debugging purposes (this works fine)
echo $GLOBALS["a"]["b"]["c"] . "\n";
// Try to evaluate the accessor (this will fail)
echo $GLOBALS[$variablePath[0]][$variablePath[1]][$variablePath[2]];
?>
以下是我编写的一些代码,通过点符号访问$\u会话变量。你应该能够相当容易地翻译它
<?php
$key = "a.b.c";
$key_bits = explode(".", $key);
$cursor = $_SESSION;
foreach ($key_bits as $bit)
{
if (isset($cursor[$bit]))
{
$cursor = $cursor[$bit];
}
else
{
return false;
}
}
return $cursor;
这里还有一个使用helper函数的解决方案:
function GetValue($path, $scope = false){
$result = !empty($scope) ? $scope : $GLOBALS;
// make notation uniform
$path = preg_replace('/\[([^\]]+)\]/', '.$1', $path); // arrays
$path = str_replace('->', '.', $path); // object properties
foreach (explode('.', $path) as $part){
if (is_array($result) && array_key_exists($part, $result)){
$result = $result[$part];
} else if (is_object($result) && property_exists($result, $part)){
$result = $result->$part;
} else {
return false; // erroneous
}
}
return $result;
}
用法和示例:
// Some example data
$GLOBALS["a"] = array(
'b' => array(
'c' => 'foo',
'd' => 'bar',
),
'e' => (object)array(
'f' => 'foo',
'g' => 'bar'
)
);
$bar = array(
'a' => $GLOBALS['a']
);
echo $GLOBALS['a']['b']['c'] . "\n"; // original
// $GLOBALS['a']['b']['c']
echo GetValue('a.b.c') . "\n"; // traditional usage
// $GLOBALS['a']['b']['c']
echo GetValue('a[b][c]') . "\n"; // different notation
// $bar['a']['b']['c']
echo GetValue('a.b.c', $bar) . "\n"; // changing root object
// $GLOBALS['a']['e']->f
echo GetValue('a[e]->f') . "\n"; // object notation
“[”不需要转义字符吗?@VeNoMiS:如果我用反斜杠转义,那个反斜杠也会出现在错误消息中。所以我想,不,我是说\[顺便说一句,我试过了:看,这是有道理的。你能推荐一种替代方法吗?实际上,我在这里找到了一个解决方案:感谢路径中的元素数量是可变的,这就是为什么我首先要寻找这个评估。
function GetValue($path, $scope = false){
$result = !empty($scope) ? $scope : $GLOBALS;
// make notation uniform
$path = preg_replace('/\[([^\]]+)\]/', '.$1', $path); // arrays
$path = str_replace('->', '.', $path); // object properties
foreach (explode('.', $path) as $part){
if (is_array($result) && array_key_exists($part, $result)){
$result = $result[$part];
} else if (is_object($result) && property_exists($result, $part)){
$result = $result->$part;
} else {
return false; // erroneous
}
}
return $result;
}
// Some example data
$GLOBALS["a"] = array(
'b' => array(
'c' => 'foo',
'd' => 'bar',
),
'e' => (object)array(
'f' => 'foo',
'g' => 'bar'
)
);
$bar = array(
'a' => $GLOBALS['a']
);
echo $GLOBALS['a']['b']['c'] . "\n"; // original
// $GLOBALS['a']['b']['c']
echo GetValue('a.b.c') . "\n"; // traditional usage
// $GLOBALS['a']['b']['c']
echo GetValue('a[b][c]') . "\n"; // different notation
// $bar['a']['b']['c']
echo GetValue('a.b.c', $bar) . "\n"; // changing root object
// $GLOBALS['a']['e']->f
echo GetValue('a[e]->f') . "\n"; // object notation