多维数组未定义索引问题

发布于 2024-12-01 03:38:47 字数 550 浏览 0 评论 0原文

我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

前事休说 2024-12-08 03:38:47

根据您的评论,问题似乎如下:

print_r 从不打印字符串键的引号。如果您没有以某种方式操作输出,那么它只能意味着单引号实际上是键的一部分。

这应该可行:

echo $chapters[0]["'title'"];

但最好修理钥匙。

从你的评论来看:

问题是我在 html 表单中使用单引号 (name="chapter[0]['photo_caption']"),更正为 name="chapter[0] [photo_caption]”解决了问题

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:

echo $chapters[0]["'title'"];

but better you fix the keys.

From your comment:

the problem was that i was using single quotes (name="chapter[0]['photo_caption']") in html form, corrected to name="chapter[0][photo_caption]" solved the problem

初见 2024-12-08 03:38:47

根据您的输出,您应该使用 $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.

落叶缤纷 2024-12-08 03:38:47

是的,我遇到了同样的问题。然后我意识到,我用错了钥匙。
实际上,我在将表单元素命名为数组时使用了引号

示例

echo "<input type='hidden' name= userprogramscholarship[$user->id]['checkstatus'] value= $val />";

- 我更正并删除了引号,如下所示

echo "<input type='hidden' name= userprogramscholarship[$user->id][checkstatus] value= $val />";

这是一个小错误。删除引号然后就可以了。

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-

echo "<input type='hidden' name= userprogramscholarship[$user->id]['checkstatus'] value= $val />";

I corrected and removed quotes as below

echo "<input type='hidden' name= userprogramscholarship[$user->id][checkstatus] value= $val />";

It was minor mistake. Removed quotes and it worked then.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文