访问使用 php 动态创建的数组


Accessing arrays created dynamically with php

在执行过程中,所有错误都保存到一个数组中,如下所示:

$this->errors[$section] = $e->getMessage();
问题是,在

执行之后,我必须发布所有部分,包括那些尝试与错误连接错误的部分。当我尝试:

echo $this->errors[$section];

这是我得到的警告:

Warning: Illegal offset type in ....

动态访问 PHP 数组中描述的解决方案没有帮助。

使用前检查索引是否有效:

if (array_key_exists($section, $this->errors) ) { 
    echo $this->errors[$section]; 
}
如果你

没有设置$section,那么PHP将不知道返回什么。

假设您的部分是具有某种 ID 号或字符串的数组:

$errors[$section['id']] = $e->getMessage();
...
foreach($errors as $error){
    print_r($error);
}

我有一种感觉,您的问题在于$section不再设置为第二个代码块中您想要的内容。