我需要一些帮助来解析PHP中的JSON。
下面的JSON位于变量$ response
中[
{
"id": "6b3c7ccf7291170020b657aa06b6cfd3",
"app": "passbook",
"os": "ios",
"registered": "2017-01-10T01:19:27Z",
"pushToken": "bf32dc858e02bddbbd21df9e95b094e5b0edb6cf0ebd8b6c757799866c3ce5e3",
"lastSeen": "2017-04-19T21:07:03Z"
},
{
"id": "9ac290d5186a081c5498c604db9f8d59",
"app": "passbook",
"os": "ios",
"registered": "2017-02-16T16:15:02Z",
"pushToken": "702c5f8077bda33d2cbafbcabba78b230668877ef08abbff5420017be221700d",
"lastSeen": "2017-02-16T16:15:02Z"
}
]
我需要从两者中获取id和app详细信息并显示它。
我的代码是
$json = json_decode($response,true);
foreach ($json as $row) {
echo $row['id'];
echo $row['app'];
}
我收到错误
PHP警告:为foreach()提供的参数无效
答案 0 :(得分:1)
你的代码对我来说很好。
为什么不为你工作的可能原因: -
1.可能您的json
变量不是$response
。
2. $resonse
有更多数据,数据可能有问题(在这种情况下向我们展示完整数据)。
3.在$json = json_decode($response,true);
之前,可能还有一些其他代码已经在处理$response
。
请查看以下内容: -
<?php
$response = '[
{
"id": "6b3c7ccf7291170020b657aa06b6cfd3",
"app": "passbook",
"os": "ios",
"registered": "2017-01-10T01:19:27Z",
"pushToken": "bf32dc858e02bddbbd21df9e95b094e5b0edb6cf0ebd8b6c757799866c3ce5e3",
"lastSeen": "2017-04-19T21:07:03Z"
},
{
"id": "9ac290d5186a081c5498c604db9f8d59",
"app": "passbook",
"os": "ios",
"registered": "2017-02-16T16:15:02Z",
"pushToken": "702c5f8077bda33d2cbafbcabba78b230668877ef08abbff5420017be221700d",
"lastSeen": "2017-02-16T16:15:02Z"
}
]';
$json = json_decode($response,true);
echo "<pre/>";print_r($json);
foreach ($json as $row) {
echo $row['id'];
echo PHP_EOL;
echo $row['app'];
echo PHP_EOL;
}
输出: - https://eval.in/784126