将JSON字符串内容解析为PHP数组 [英] Parse JSON string contents into PHP Array

查看:129
本文介绍了将JSON字符串内容解析为PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解析JSON中的字符串,但不确定如何执行此操作.这是我要解析为PHP数组的字符串的示例.

I am trying to parse a string in JSON, but not sure how to go about this. This is an example of the string I am trying to parse into a PHP array.

$json = '{"id":1,"name":"foo","email":"foo@test.com"}';  

是否有一些库可以接受id,名称和电子邮件并将其放入数组中?

Is there some library that can take the id, name, and email and put it into an array?

推荐答案

可以使用 json_decode() 完成将第二个参数设置为true,因为您需要数组而不是对象.

It can be done with json_decode(), be sure to set the second argument to true because you want an array rather than an object.

$array = json_decode($json, true); // decode json

输出:

Array
(
    [id] => 1
    [name] => foo
    [email] => foo@test.com
)

这篇关于将JSON字符串内容解析为PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆