使用 PHP 在 JSON 中细分数据



我怎样才能优雅地将以下内容转换为JSON,以便"data"中的每个元素(即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",
    }
}

假设你的意思是你已经有一个 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将是一个关联数组。

最新更新