Get php array from jquery serialize function sended value

六月ゝ 毕业季﹏ 提交于 2019-12-24 09:30:11

问题


I make a AJAX call, and sends the argument equal to forms's serialize-d value.

var form_data= $('#some-form').serialize();
var data = {
                    action: 'my-action',
                    data: form_data
                };
$.post(my-file.php,data...){...}

So in my php file var I have a $_POST['data'] = arg1=value1&arg2[arg2_1]=value2... and so on.

It can be a long string with unlimited number of arguments and unlimited depth level.

So the question - is there any function in php, to make such string to an Associative array like this

$my_post[arg1]=value1;
$my_post[arg_2][arg2_1]=value2;
...

Or I need to write that function myself?


回答1:


Use parse_str():

parse_str($_POST['data'], $my_post);



回答2:


You can use parse_str() as described here: PHP unserializing a JS serialized string of variables



来源:https://stackoverflow.com/questions/19083542/get-php-array-from-jquery-serialize-function-sended-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!