[英]Extract JSON Data with PHP
Suppose below is my JSON data假设下面是我的 JSON 数据
{"pricing": {
"com": {
"addons": {
"dns": true,
"email": true,
"idprotect": true
},
"org": {
"addons": {
"dns": true,
"email": true,
"idprotect": true
},
"net": {
"addons": {
"dns": true,
"email": true,
"idprotect": true
}
}}
I want to display only (com, org, net) from above JSON.我只想从 JSON 上方显示(com、org、net)。 How can we do that?我们怎么做?
An option to do this is to use json_decode and pass true
for the second parameter to convert the returned objects into associative arrays.这样做的一个选项是使用json_decode并为第二个参数传递true
以将返回的对象转换为关联数组。
To display only the keys you could loop $output["pricing"]
using a foreach and display the keys:要仅显示键,您可以使用foreach循环$output["pricing"]
并显示键:
$json = '{"pricing": {"com": {"addons": {"dns": true,"email": true,"idprotect": true}},"org": {"addons": {"dns": true,"email": true,"idprotect": true}},"net": {"addons": {"dns": true,"email": true,"idprotect": true}}}}';
$output = json_decode($json, true);
foreach ($output["pricing"] as $key => $value) {
echo $key . "<br>";
}
Another way could be to get the array_keys and loop them:另一种方法是获取array_keys并循环它们:
foreach (array_keys($output["pricing"]) as $key) {
echo $key . "<br>";
}
First u had a incorrect JSON format check it out.首先你有一个不正确的 JSON 格式检查出来。 I think this solution might help you!!我认为这个解决方案可能对你有帮助!!
$json = '{"pricing": {"com": {"addons": {"dns": true,"email": true,"idprotect": true}},"org": {"addons": {"dns": true,"email": true,"idprotect": true}},"net": {"addons": {"dns": true,"email": true,"idprotect": true}}}}';
$output = json_decode($json);
print_r($output->pricing->com->addons);
print_r($output->pricing->org->addons);
print_r($output->pricing->net->addons);
You will get something like this !!你会得到这样的东西!!
stdClass Object ( [dns] => 1 [email] => 1 [idprotect] => 1 )
stdClass Object ( [dns] => 1 [email] => 1 [idprotect] => 1 )
stdClass Object ( [dns] => 1 [email] => 1 [idprotect] => 1 )
Are you looking for this ??你在找这个吗??
$json = '{"pricing": {"com": {"addons": {"dns": true,"email": true,"idprotect": true}},"org": {"addons": {"dns": true,"email": true,"idprotect": true}},"net": {"addons": {"dns": true,"email": true,"idprotect": true}}}}';
$output = json_decode($json,true);
echo implode(",",array_keys($output["pricing"]));
com,org,net com,org,net
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.