我正在尝试提交大型表单时必须提高max_input_vars参数。我的想法是将表单jsonify,创建第二个表单,其中包含单个字段,将jsonified字符串放入单个字段,提交,解码为数组,然后用解码数据替换POST数组。我将其插入到现有系统中,因此为了实现这一点,我需要新数组具有与原始帖子相同的结构。但是,我遇到了一个问题,无论我如何对数据进行编码,我都会丢失超出数组第一深度的信息和结构。我已尝试使用此库对表单进行编码:
https://github.com/macek/jquery-serialize-object
与jQuery(form).serializeArray()一样,每个都提供以自己无意的方式解码的结果。当我不截取表单并使用print_r($ _ POST)转储它时,这就是原始帖子数据的样子:
Array
(
[closedpostboxesnonce] => 9d9dc8fa74
[meta-box-order-nonce] => 520ef5d263
[update-nav-menu-nonce] => cfea78920d
[_wp_http_referer] => /wp-admin/nav-menus.php
[action] => update
[menu] => 2
[menu-name] => Left Sidebar Menu
[save_menu] => Save Menu
[qtranslate-fields] => Array
(
[menu-item-title] => Array
(
[3871] => Array
(
[en] => Knowledge Center
[ja] => Knowledge Center
[ko] => Knowledge Center
[zh] => Knowledge Center
[qtranslate-separator] => [
)
当我使用上面的jquery-serialize-object库对表单数据进行编码,然后用json_decode解码它时的数据($ _ POST [" allinputs"],true):
Array
(
[closedpostboxesnonce] => 9d9dc8fa74
[meta-box-order-nonce] => 520ef5d263
[update-nav-menu-nonce] => cfea78920d
[_wp_http_referer] => /wp-admin/nav-menus.php
[action] => update
[menu] => 2
[menu-name] => Left Sidebar Menu
[qtranslate-fields[menu-item-title][3871][en]] => Knowledge Center
[qtranslate-fields[menu-item-title][3871][ja]] => Knowledge Center
[qtranslate-fields[menu-item-title][3871][ko]] => Knowledge Center
[qtranslate-fields[menu-item-title][3871][zh]] => Knowledge Center
使用jQuery(form).serializeArray()进行编码,$ allinputs =(array)(json_decode($ _ POST [" allinputs"]));解码:
Array
(
[0] => stdClass Object
(
[name] => closedpostboxesnonce
[value] => 9d9dc8fa74
)
[1] => stdClass Object
(
[name] => meta-box-order-nonce
[value] => 520ef5d263
)
[2] => stdClass Object
(
[name] => update-nav-menu-nonce
[value] => cfea78920d
)
有没有办法在保留原始结构的同时对数据进行编码然后解码?
编辑:使用@ think-win-win建议传递true作为json_decode的第二个参数产生:
Array
(
[0] => Array
(
[name] => closedpostboxesnonce
[value] => 9d9dc8fa74
)
[1] => Array
(
[name] => meta-box-order-nonce
[value] => 520ef5d263
)
[2] => Array
(
[name] => update-nav-menu-nonce
[value] => cfea78920d
)
这是我尝试过的其中一个,还有其他几个,但我觉得这个帖子太长了,无法将它们全部包括在内。上面的一个失去了数组顶层的关联性,除了将更深层次转换为:
[7] => Array
(
[name] => qtranslate-fields[menu-item-title][3871][en]
[value] => Knowledge Center
)
[8] => Array
(
[name] => qtranslate-fields[menu-item-title][3871][ja]
[value] => Knowledge Center
)