简体   繁体   English

如何从该序列化的字符串创建PHP数组?

[英]How can I create a PHP array from this serialized string?

I have a string which looks like this: 我有一个看起来像这样的字符串:

id_company=57&name=&address=&zip=&place=&phone=&mobile=&email=&birthdate=&birthplace=&bsn=&driver_expires=&role=&date_started=&contract=Tijdelijk&id_type=Paspoort&id_number=&id_expires=

Above example is empty for the most part (except id_company). 上面的示例在大多数情况下为空(id_company除外)。

I serialize my form in jquery like this: 我像这样在jquery中序列化表单:

var $serialized = $form.serialize();

And in my php I do this: 在我的PHP中,我这样做:

$jsonemployee = unserialize($_POST['serialized']);

After that I try to print it because I expect an array: 之后,我尝试打印它,因为我期望一个数组:

echo '<pre>';
print_r($jsonemployee);
echo '</pre>';

But this shows me nothing. 但这什么也没给我显示。

I am 100% sure all data is passed, in my networktab I see the string being posted and if I just echo the string, it shows the string. 我100%肯定所有数据都已传递,在我的networktab中,我看到正在发布的字符串,如果我只是回显该字符串,它将显示该字符串。

How can I create a PHP array from that string? 如何从该字符串创建PHP数组?

unserialize is not for unserialising that format, it's the counterpart to serialize , which produces a very different result. unserialize并不是要对该格式进行反serialize ,它是serialize的对应物,它会产生非常不同的结果。 To parse a URL-encoded string in PHP, use parse_str . 要在PHP中解析URL编码的字符串,请使用parse_str

parse_str($_POST['serialized'], $result);
var_dump($result);

Of course, it's somewhat bizarre that you're sending a URL encoded string as $_POST['serialized'] … You should send it as the one and only request body, and PHP will automatically parse it into $_POST and you won't have to do any of this. 当然,将URL编码的字符串发送为$_POST['serialized']有点奇怪,您应该将它作为一个唯一的请求正文发送,PHP会自动将其解析为$_POST而您不会必须做任何事情。

you just need to convert this string into an array by doing : 您只需要通过执行以下操作将该字符串转换为数组:

$myArray = explode("&",$_POST['serialized']);

i think that will work for you. 我认为这将为您工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM