PHP嵌套数组产生未定义的索引错误-我做错了什么


PHP nested array yielding undefined index error - what am I doing wrong?

我正在向控制器发送POST数组。阵列看起来是这样的:

Array
(
[event] => Array
    (
        [0] => Array
            (
                ['publishStart'] => 2013-12-10
                ['eventStart'] => 2014-05-05
                ['eventEnd'] => 2014-05-10
                ['timeStart'] => 
                ['timeEnd'] => 
                ['location_id'] => 1
                ['id'] => 65774
            )

其中有几个块,即[事件][1]。[event][2]等。我正试图在$_POST['event']上运行foreach循环,并可以确认在每次迭代中,$event包含以下内容:

Array
(
    ['publishStart'] => 2013-12-10
    ['eventStart'] => 2014-05-05
    ['eventEnd'] => 2014-05-10
    ['timeStart'] => 
    ['timeEnd'] => 
    ['location_id'] => 1
    ['id'] => 65774
)

现在,问题来了。您可以在上面的两个数组中看到一个名为"id"的键,该键具有相应的值。然而,以下代码返回一系列"未定义索引"错误:

foreach ($_POST['event'] as $event)
{   
    echo $event['id'];
    exit();
}

我到底做错了什么?

您的数组索引似乎包含了单引号。

尝试

echo $event["'id'"];