PHP 从 post 数组中获取值


PHP get value from post array

我正在发布一个来自 html 表单的数组,其中包含以下输入标签:

<input type="text" name="new_service[1]['category']">

我得到这个数组:

Array
(
    [1] => Array
        (
            ['category'] => 1
            ['description'] => asdasd
            ['price'] => asdasd
            ['time'] => 1
        )
)

当我尝试获取类别值时

echo $_POST['new_service'][1]['category']; 

我收到此错误消息:

Notice: Undefined index: category

我做错了什么?

您以这种方式创建的索引不是类别,而是"类别",即您创建一个也带有单引号的索引。

因此,要呼应您想要的内容,您应该使用:

echo $_POST['new_service'][1]["类别"];

只需使用 $_POST['new_service'];然后循环