Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用PHP提取JSON数据_Php_Arrays_Json - Fatal编程技术网

用PHP提取JSON数据

用PHP提取JSON数据,php,arrays,json,Php,Arrays,Json,假设下面是我的JSON数据 {"pricing": { "com": { "addons": { "dns": true, "email": true, "idprotect": true }, "org": { "addons": { "dns": true, "em

假设下面是我的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
       }
}}
我只想显示上面JSON中的(com、org、net)。我们如何做到这一点?

您可以轻松做到:

$parsed_json= json_decode("your raw data here");
$com = $parsed_json["com"];
$org = $parsed_json["org"];
$net = $parsed_json["net"];

您可以在PHP文档网站上了解更多信息。

首先,您有一个不正确的JSON格式,请检查它。我认为这个解决方案可能会对你有所帮助

$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);
你会得到像这样的东西

stdClass Object ( [dns] => 1 [email] => 1 [idprotect] => 1 ) 
stdClass Object ( [dns] => 1 [email] => 1 [idprotect] => 1 ) 
stdClass Object ( [dns] => 1 [email] => 1 [idprotect] => 1 )

执行此操作的一个选项是对第二个参数使用并传递
true
,以将返回的对象转换为关联数组

要仅显示按键,您可以使用a循环
$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>";
}
$json='{“定价”:{“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($key=>$value的输出[“定价”]{
回声$key.“
”; }
另一种方法可以是获取并循环它们:

foreach (array_keys($output["pricing"]) as $key) {
    echo $key . "<br>";
}
foreach(数组_键($output[“pricing”])作为$key){
回声$key.“
”; }
你在找这个吗

 $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

我只想echo-com,org,net
foreach($output->pricing as$key=>$value){echo$key;}
它会工作吗?你只想显示key
com
等等?是的@ThefourthbirdAlso,我们能做些什么使它成为一个PHP数组?它返回为'array([0]=>com[1]=>org[2]=>net'如果我们想返回为array呢('com'、'org'、'net')?只有没有数字键的。请检查