PHP从post数组中获取值

时间:2016-01-30 19:17:43

标签: php arrays

我使用以下输入标记从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

我做错了什么?

2 个答案:

答案 0 :(得分:3)

您以这种方式创建的索引不是类别,而是&#39;类别&#39;即你创建一个也有单引号的索引。

所以要回应你想要的东西你应该使用:

  

echo $ _POST [&#39; new_service&#39;] [1] [&#34;&#39; category&#39;&#34;];

答案 1 :(得分:0)

只需使用$ _POST [&#39; new_service&#39;];然后循环