如何在PHP中解析此JSON?
问题描述:
它存储在MongoDB中,并通过json_decode.
It's stored inside MongoDB and passed to my view file with json_decode.
使用PHP,如何从内部获取值?
Using PHP, how can I grab the values from within?
"environment" : {
"_id" : "QU",
"name" : "QA Unstable",
"streams" : "unstable",
"hosts" : [
"deployclient1",
"deployclient2"
]
}
答
由于您已经知道json_decode
,因此现在可以实际回答问题了:
Now to actually answer the question since you already know of json_decode
:
使用PHP,如何从内部获取值?
Using PHP, how can I grab the values from within?
json_decode
会将JSON字符串评估为PHP中的一个对象(默认情况下),这意味着您可以使用基本的动态访问语法来获取您的值,即获取_id
:
json_decode
will evaluate the JSON string into a object in PHP (by default) which means you can use basic dynamic accession syntax to get to your values, i.e. to get _id
:
$object->environment->_id;
或主持人:
$object->environment->hosts[0]
这将返回:deployclient1