Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP数组中循环,错误日志显示未定义常量_Php_Arrays - Fatal编程技术网

在PHP数组中循环,错误日志显示未定义常量

在PHP数组中循环,错误日志显示未定义常量,php,arrays,Php,Arrays,我有一个PHP数组$data,看起来像这样 Array ( [section] => Array ( [345dfg] => Array ( [test] => Array ( [name] => John

我有一个PHP数组$data,看起来像这样

Array
(
    [section] => Array
        (
            [345dfg] => Array
                (
                    [test] => Array
                        (
                            [name] => John
                        )
                )
            [567fghj] => Array
                (
                    [test] => Array
                        (
                            [name] => Tom
                        )
                )
        )
    [othersection] => Array
        (
            [result] => 2
        )
)
我正在尝试循环查看
部分中的每个项目
,所以我正在这样做

foreach ($data[section] as $section) {

    echo $section[test][name];

}
它工作正常,但在我的错误日志中我得到

PHP Warning:  Use of undefined constant section- assumed 'section' (this will throw an Error in a future version of PHP)

哪里出错了?

您需要用一个引号将数组键括起来,因为它们是字符串类型

所以

应该是

foreach ($data['section'] as $section) {
否则,如果没有
$
符号且没有单个引号,
将被视为
常量

$data['section']
的可能性:

1)
$section
作为变量:
$data[$section]

2)
section
作为常量:
$data[section]

3)
section
作为数组键(字符串):
$data['section']

用单引号括住数组键始终是一种好的做法

由于巧合,如果定义了相同的常数,则该常数的值可被视为数组键


如果未定义常量,它将显示一条警告。

您有打字错误$data['section'],可以使用单引号或双引号访问数组索引。在这个echo$section['test][“name”]中,将执行以下操作:;
foreach ($data['section'] as $section) {