【问题标题】:Unserialize non standard string反序列化非标准字符串
【发布时间】:2023-04-01 15:15:01
【问题描述】:

我有一个看起来像数组的字符串:-

'[["hello","world"],["etc","etc..."]]'

有没有办法“反序列化”它,即使它不是真正正确的序列化格式。

【问题讨论】:

  • 序列化数据有什么不正确/不正确的地方。您发布的是一个 JSON 序列化对象。
  • 我的意思是它不是 php serialize() 序列化的数组的样子。 php 的 unserialize() 函数对它不起作用。

标签: php deserialization


【解决方案1】:

您可以将其作为数组返回,因为它是 JSON:

$json = '[["hello","world"],["etc","etc..."]]';
$decoded = json_decode($json, true); // true option returns associative array
print_r($decoded);

返回:

Array
(
    [0] => Array
        (
            [0] => hello
            [1] => world
        )

    [1] => Array
        (
            [0] => etc
            [1] => etc...
        )

)

【讨论】:

    【解决方案2】:

    您可以在此字符串上使用json_decode 函数。

    【讨论】:

      猜你喜欢
      • 2015-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-10
      • 2015-11-26
      • 2018-10-22
      相关资源
      最近更新 更多