如何使用PHP从FCC API的JSON输出中获取县值?

时间:2019-04-30 18:16:42

标签: php json php-curl

查看大量示例后,我仍然没有得到我真正需要的JSON输出中的数据。输出看起来像这样;

{
  "input": {
    "lat": 39.2,
    "lon": -94.6
  },
  "results": [
    {
      "block_fips": "290470212042002",
      "bbox": [
        -94.601125,
        39.195763,
        -94.590245,
        39.20315
      ],
      "county_fips": "29047",
      "county_name": "Clay",
      "state_fips": "29",
      "state_code": "MO",
      "state_name": "Missouri",
      "block_pop_2015": 288,
      "amt": "AMT004",
      "bea": "BEA099",
      "bta": "BTA226",
      "cma": "CMA024",
      "eag": "EAG005",
      "ivm": "IVM024",
      "mea": "MEA029",
      "mta": "MTA034",
      "pea": "PEA030",
      "rea": "REA004",
      "rpc": "RPC004",
      "vpc": "VPC004"
    }
  ]
}

我正在尝试像这样检索county_name值;

$fccdata = json_decode($response, true); 
  $county = $fccdata['results']['county_name'];

但是什么也没有返回。我也尝试过;

$fccdata = json_decode($response, true); 
  $county = $fccdata['county_name'];

同样缺乏成功。

1 个答案:

答案 0 :(得分:0)

0之后您丢失了results索引

$jsonToArray = json_decode($response,true);
$countryName = $jsonToArray['results'][0]['county_name'];