如何反序列化数组字符串


How to unserialize an array string

这就是我创建数组的方式

var list = $("#sortable").sortable('toArray');
$.ajax({
       type: 'POST',
       url: " some path ",
       data: { "list" : JSON.stringify(list) },
       success: function(data) {}
});

在数据库中看起来像这样

["34","37","38","40","41","42","43","44","45","48","49","50"]

不,我想在 php 中将其用作数组。如何将此字符串转换为数组?我尝试了unserialize()方法,但这似乎不是要走的路。

您可以使用

json_decode函数:

$arr = json_decode($_POST["list"])

不要字符串化 json 对象,让 jQuery 为你处理。

$.ajax({
       type: 'POST',
       url: " some path ",
       data: { "list" : list }, // here, it's not necessary to stringify the json object.
       success: function(data) {}
});

然后你通过 $_POST['list'] 获得 php 中的数组。