PHP将序列化的JSON转换为数组

时间:2017-11-24 09:40:02

标签: php json

PHP:

$ids = json_decode(["1","3"]);
$ID = implode(",", array_map('intval', $ids));
print_r($ID);

任何人都可以告诉我如何将序列化的JSON转换为数组?为什么这个程序不起作用?

2 个答案:

答案 0 :(得分:1)

如果您查看json_decode的文档,您会看到$ json必须是字符串类型。

  

混合json_decode(字符串$ json [,bool $ assoc = false [,int $ depth   = 512 [,int $ options = 0]]])

所以你的代码应该是这样的:

$ids = json_decode('["1","3"]');
$ID = implode(",", array_map('intval', $ids));
print_r($ID);

答案 1 :(得分:0)

<?php


$ids = json_decode(json_encode(["1","3"],true),true);
$ID = implode(",", array_map('intval', $ids));
print_r($ID);

您需要先将其编码为json,然后对其进行解码。不要忘记,true这很重要。

输出为1,3