-1

我知道这是一个基本问题,但我无法弄清楚如何实际做到这一点。我已经阅读了无数关于它的教程,但它们似乎不起作用。

var_dump($google_check);

返回以下内容:

string(488) "{
  "matches": [
    {
      "threatType": "MALWARE",
      "platformType": "LINUX",
      "threat": {
        "url": "http://malware.testing.google.test/testing/malware/"
      },
      "cacheDuration": "300s",
      "threatEntryType": "URL"
    },
    {
      "threatType": "MALWARE",
      "platformType": "LINUX",
      "threat": {
        "url": "http://malware.testing.google.test/testing/malware/"
      },
      "cacheDuration": "300s",
      "threatEntryType": "URL"
    } 
  ]
} 
"

我想从数组中回显结果,所以像

echo $google_check[0][matches][threat];
echo $google_check[1][matches][threat];

问题是这会在匹配和威胁上返回非法偏移,并且只回显单个字符 {

我究竟做错了什么?如何在不转储整个数组的情况下回显该数组的结果?

4

1 回答 1

4

您收到的响应是 json 格式,因此您需要先 json_decode 响应。

$decoded = json_decode($google_check, true);

然后你可以像数组一样访问它

echo $decoded['matches'][0]['threat'];
echo $decoded['matches'][1]['threat'];

如果你想要 url 值,你需要这样做。

echo $decoded['matches'][0]['threat']['url'];
echo $decoded['matches'][1]['threat']['url'];

另请注意,在查看非数字的数组键时,您需要用引号括起来(例如,$decoded['matches'] 而不是 $decoded[matches])。

这是关于json的快速解释

https://www.tutorialspoint.com/json/json_php_example.htm

于 2016-12-21T01:49:11.450 回答