【问题标题】:How to unserialize an array string如何反序列化数组字符串
【发布时间】:2012-06-27 07:35:54
【问题描述】:

这就是我创建数组的方式

var list = $("#sortable").sortable('toArray');

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

在 DB 中是这样的

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

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

【问题讨论】:

  • PHP 的 unserialize 用于使用 PHP 的 serialize 序列化的字符串,您实际上是在发送 JSON,所以,json_decode

标签: php jquery serialization


【解决方案1】:

您可以使用json_decode 函数:

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

【讨论】:

    【解决方案2】:

    不要对 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中的数组。

    【讨论】:

      猜你喜欢
      • 2017-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-22
      • 2022-09-23
      相关资源
      最近更新 更多