Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.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_Json - Fatal编程技术网

使用PHP获取JSON中的更改项

使用PHP获取JSON中的更改项,php,json,Php,Json,因此,我有以下JSON: "response": { "status": 1, "httpStatus": 200, "data": { "16322": { "SignupAnswer": { "id": "16322", "question_id": "4", "responder_type": "affiliate", "answer": "https://w

因此,我有以下JSON:

"response": {
    "status": 1,
    "httpStatus": 200,
    "data": {
      "16322": {
        "SignupAnswer": {
          "id": "16322",
          "question_id": "4",
          "responder_type": "affiliate",
          "answer": "https://www.hoopladoopla.com",
          "responder_user_id": null,
          "ref_id": null,
          "responder_id": "3532",
          "modified": "2014-03-26 13:00:23",
          "question": "URL de tu sitio web",
          "type": "affiliate",
          "status": "active"
        }
      }
我需要得到response->data->16322,并将其中的信息转换为php变量,这样我就可以将它们保存到SQL数据库中,但我需要一些通用规则来完成这项工作,而不必指定最后一项的名称,因为它会不断变化

很可能我一点也不清楚(因为我不知道我在寻找什么,只知道结果),但我希望有人能理解我要去哪里,并找到一种方法来做到这一点


有什么想法吗?

这不是有效的JSON,它缺少一些
{
}
,但如果是:

$array = json_decode($data, true);
$result = reset($array['response']['data']);
print_r($result);
或PHP>=5.4.0

$result = reset(json_decode($data, true)['response']['data']);
或者如果
数据有多个条目
,则:

foreach(json_decode($data, true)['response']['data'] as $key => $array) {
    //do something with $array['id'] or whatever
}
你可以随心所欲

$j = '{"response": {
    "status": 1,
    "httpStatus": 200,
    "data": {
      "16322": {
        "SignupAnswer": {
          "id": "16322",
          "question_id": "4",
          "responder_type": "affiliate",
          "answer": "https://www.hoopladoopla.com",
          "responder_user_id": null,
          "ref_id": null,
          "responder_id": "3532",
          "modified": "2014-03-26 13:00:23",
          "question": "URL de tu sitio web",
          "type": "affiliate",
          "status": "active"
        }
      }
     }
    }
}';

$data = json_decode($j,true);

foreach($data["response"]["data"] as $key=>$val){
  // $val will hold the data for the key and can do a loop to get the elements and data
  // $key will be 16322 and it could be anything which is assigned to $key
  print_r($val); 
}

对上述结果使用json_decode,它将返回一个关联数组。然后打印R($array),查看生成的数组结构,然后相应地访问其中的数据。