0

如何优雅地将以下内容转换为 JSON,以便“数据”中的每个元素(即 1、2、3、4 和 5)都是独立的,并且无需使用 str_replace 或 preg_replace 即可轻松遍历?

{
   "status":"success",
   "data":{
      "1":"this is an element",
      "2":"this is an element",
      "3":"this is an element",
      "4":"this is an element",
      "5":"this is an element",
    }
}
4

1 回答 1

4

假设您的意思是您已经有一个 JSON 字符串,请使用json_decode()并提供第二个参数true以获取解码后的 JSON 的关联数组。

$str = '{"status":"success","data":{"1":"this is an element","2":"this is an element","3":"this is an element","4":"this is an element","5":"this is an element"}}'
$json = json_decode($str, true);

该变量$json将是一个关联数组。

于 2013-10-15T03:37:33.270 回答