关联数组返回未定义的索引,即使索引已明确定义

Associative array returns undefined index even though index is clearly defined

提问人:Madara's Ghost 提问时间:4/13/2012 更新时间:4/13/2012 访问量:1407

问:

我有以下(精简)代码:

$question = fetch_question($conn, $current_question);
var_dump($question);
echo $question["_id"];

fetch_question()使用数据库连接检索 上的数组。$conn$current_question

这给了我以下输出:

array(1) {
  [0]=>
  array(7) {
    ["_id"]=>
    string(1) "1"
    ["question"]=>
    string(10) "What's 2+2"
    ["answer1"]=>
    string(1) "1"
    ["answer2"]=>
    string(1) "2"
    ["answer3"]=>
    string(1) "3"
    ["answer4"]=>
    string(1) "4"
    ["is_right"]=>
    string(1) "4"
  }
}
<br />
<b>Notice</b>:  Undefined index: _id in <b>D:\Websites\htdocs\tests\general.php</b> on line <b>76</b><br />

现在我发现这很奇特,定义很清楚,我不知道是什么让PHP表现得像这样!_id

任何帮助将不胜感激!

php 关联数组

评论


答:

1赞 Madara's Ghost 4/13/2012 #1

只有在看到 StackOverflow 上的输出后,我才意识到我实际上需要.$question[0]["_id"]

好吧,我们都知道这些小害虫。