Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法在Wordpress中将数组用作表单名称_Php_Wordpress_Forms - Fatal编程技术网

Php 无法在Wordpress中将数组用作表单名称

Php 无法在Wordpress中将数组用作表单名称,php,wordpress,forms,Php,Wordpress,Forms,在PHP世界中,在HTML表单中按照这些行进行操作是完全正常的: <input type="text" name="data[name][first]" /> <input type="text" name="data[name][last]" /> <input type="text" name="data[address][street]" /> <input type="text" name="data[address][city]" />

在PHP世界中,在HTML表单中按照这些行进行操作是完全正常的:

<input type="text" name="data[name][first]" />
<input type="text" name="data[name][last]" />
<input type="text" name="data[address][street]" />
<input type="text" name="data[address][city]" />
然后,您可以在var中使用这些名称来访问它们,即,
$firstName=$\u POST['data']['name']['first']

我的问题是,这在Wordpress中根本不起作用。我得到的错误是
trim()
函数不能用于数组

我将“为什么”追溯到
WP\u query
对象的
parse\u query
函数中的以下代码:

$qv['p'] =  absint($qv['p']);
        $qv['page_id'] =  absint($qv['page_id']);
        $qv['year'] = absint($qv['year']);
        $qv['monthnum'] = absint($qv['monthnum']);
        $qv['day'] = absint($qv['day']);
        $qv['w'] = absint($qv['w']);
        $qv['m'] = absint($qv['m']);
        $qv['paged'] = absint($qv['paged']);
        $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
        $qv['pagename'] = trim( $qv['pagename'] );
        $qv['name'] = trim( $qv['name'] ); /* Throws trim() error on array! */
        if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
        if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
        if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
        if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']);

有人能想出一种在WordPress中正确使用字段名称数组的方法吗?在WP之外似乎是一种常见的做法,但在WP内部却不可能?也许有一个替代表单名称的“最佳实践”?

当我更改字段的名称时,特别是数组的“基”时,解决方案出现了

在我提供的示例中,它是
数据
,然后有几个元素。在本例中,
data
碰巧也是我在插件中向WordPress注册的自定义帖子类型。我不知道到底发生了什么,也不知道为什么,但在很长的一段时间里,它把我的数组和应该查询的数组混淆了,很可能是因为名称冲突


问题解决了

您能否在引发该异常的行之前发布一个
var\u dump($\u post)
?也许WordPress正在做一些事情来发布数据并将其弄乱。如果
$qv['name']
应该是一个字符串,那么将数组传递给它是不起作用的。您希望该数组的各个部分在哪里结束?对WordPress来说,
$qv['name']
可能是一个字符串,但我希望表单的
name
元素是一个数组,即
$\u POST['data']
(其中
数据本身就是一个数组)我不清楚
$\u POST
$qv
之间的关系:您是将
$\u POST
传递给一个函数,还是每个页面都会处理这个问题?也许您只需要为自定义表单参数选择在Wordpress中没有不同含义的名称?老实说,我也不确定两者之间的关系-为什么通过Wordpress中的表单发送数据会走“查询”路线。我认为这与
$\u POST
有关,但这只是因为这正是我所期望的,并且在页面重新加载时尝试访问它。这里可能还有别的东西。
$qv['p'] =  absint($qv['p']);
        $qv['page_id'] =  absint($qv['page_id']);
        $qv['year'] = absint($qv['year']);
        $qv['monthnum'] = absint($qv['monthnum']);
        $qv['day'] = absint($qv['day']);
        $qv['w'] = absint($qv['w']);
        $qv['m'] = absint($qv['m']);
        $qv['paged'] = absint($qv['paged']);
        $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
        $qv['pagename'] = trim( $qv['pagename'] );
        $qv['name'] = trim( $qv['name'] ); /* Throws trim() error on array! */
        if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
        if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
        if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
        if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']);