多维数组未定义索引问题
我从 HTML 表单中获取一个多维数组。当我想获取单个值时,例如
$chapters = $_POST["chapters"];
echo $chapters[0]["title"];
它显示未定义索引标题
。
当我打印数组时,它显示为
Array
(
[chapters] => Array
(
[0] => Array
(
['title'] => this is title
['text'] => this is text
['photo'] => this is photo source
['photo_caption'] => photo caption
)
)
)
I am getting a multidimensional array from an HTML form. When I want to get a single value, e.g.
$chapters = $_POST["chapters"];
echo $chapters[0]["title"];
it says undefined index title
.
When I print the array, it shows as
Array
(
[chapters] => Array
(
[0] => Array
(
['title'] => this is title
['text'] => this is text
['photo'] => this is photo source
['photo_caption'] => photo caption
)
)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您的评论,问题似乎如下:
print_r
从不打印字符串键的引号。如果您没有以某种方式操作输出,那么它只能意味着单引号实际上是键的一部分。这应该可行:
但最好修理钥匙。
从你的评论来看:
Based on your comments, the problem seemed to be following:
print_r
never prints quotes for string keys. If you have not manipulated the output somehow, then it can only mean that the single quotes are actually part of the key.This should work:
but better you fix the keys.
From your comment:
根据您的输出,您应该使用
$chapters["chapters"][0]["title"]
。请注意,输出中有 3 个嵌套数组,因此您需要深入 3 层才能获取值。
According to your output, you should be using
$chapters["chapters"][0]["title"]
.Note that you have 3 nested arrays in your output, therefore you'll need to go 3 levels deep to get your value.
是的,我遇到了同样的问题。然后我意识到,我用错了钥匙。
实际上,我在将表单元素命名为数组时使用了引号
示例
- 我更正并删除了引号,如下所示
这是一个小错误。删除引号然后就可以了。
Yeah, I faced the same problem. Then I realized, I was doing wrong with the keys.
Actually, I was using the quote while naming the form elements as an array
Example-
I corrected and removed quotes as below
It was minor mistake. Removed quotes and it worked then.