Php json_解码后的子数组结构提取

Php json_解码后的子数组结构提取,php,json,Php,Json,我是PHP新手,我正试图用它来过滤《纽约时报》文章搜索API中的信息。我仍然在努力找出如何从JSON响应中提取“web url”和“代码片段”,即使在查看了类似的内容之后也是如此。我使用json_decode将响应转换为关联数组。这是我的密码 $data = file_get_contents($queryURL); $jsondata = json_decode($data, true); if (count($jsondata) != 0) {

我是PHP新手,我正试图用它来过滤《纽约时报》文章搜索API中的信息。我仍然在努力找出如何从JSON响应中提取“web url”和“代码片段”,即使在查看了类似的内容之后也是如此。我使用json_decode将响应转换为关联数组。这是我的密码

   $data = file_get_contents($queryURL);
    $jsondata = json_decode($data, true);
    if (count($jsondata) != 0) 
    {
        foreach($jsondata['response']as $key => $value) 
        {
          echo "Key: " . $key . " Value: " . $value . " <br>";
        } 
    }

试试这个。您需要循环浏览每个文档

foreach ($jsondata['response']['docs'] as $doc) {
    echo "web_url: " . $doc['web_url'] . " snippet: " . $doc['snippet'];
}

试试这个。您需要循环浏览每个文档

foreach ($jsondata['response']['docs'] as $doc) {
    echo "web_url: " . $doc['web_url'] . " snippet: " . $doc['snippet'];
}

当您在
$jsondata['response']中循环时

然后,当您的$keymeta时(类似于其他键以及文档等),其$value是一个数组,即

array(
"hits"=> 25,
        "time"=> 332,
        "offset"=> 0
)
因此,当您尝试回显此$value时,它会将数组打印为php中回显的属性,以将数组打印为字符串“数组”

我希望这能说明你做错了什么


你可能需要这样做

foreach($jsondata['response']as $key => $value) 
        {
          // check if $jsondata['response'][$key] is an array using is_array()
          // handle as per array

        } 

当您在
$jsondata['response']中循环时,了解

然后,当您的$keymeta时(类似于其他键以及文档等),其$value是一个数组,即

array(
"hits"=> 25,
        "time"=> 332,
        "offset"=> 0
)
因此,当您尝试回显此$value时,它会将数组打印为php中回显的属性,以将数组打印为字符串“数组”

我希望这能说明你做错了什么


你可能需要这样做

foreach($jsondata['response']as $key => $value) 
        {
          // check if $jsondata['response'][$key] is an array using is_array()
          // handle as per array

        } 

了解

foreach($jsondata['response']['docs']作为$value){echo$value['web_url';echo$value['snippet'];}
foreach($jsondata['response']['docs']作为$value){echo$value['web_url';echo$value['snippet']}
?太棒了,我不知道你能做$jsondata['response'['docs']太棒了,我不知道你能做$jsondata['response']['docs']