如何使用PHP使用API json来预测数据,在这里我提到我使用的API数据样本和我正在尝试的代码,但它不打印任何东西,也没有错误。更新:下面我更新了api数据,以便更好地了解哪些是私有数据,这样我就不会分享api url。
这是代码:
{
"1": {
"ID_PP": [
"1",
"2",
"3"
],
"detail": {
"price": 16.5,
"tax": null,
"shipment": null
}
},
"2": {
"ID_PP": [
"6",
"7",
"8",
"9",
"10"
],
"detail": {
"price": 14,
"tax": null,
"shipment": null
}
},
"3": {
"ID_PP": [
"13",
"14"
],
"detail": {
"price": 6,
"tax": null,
"shipment": null
}
}
}
$url = "API URL";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$execute=curl_exec($ch);
curl_close($ch);
$return = json_decode($execute, true);
$i = 0;
foreach($return['ID_PP'] as $key=>$val) {
echo $val[$i];
$i++;}
答案 0 :(得分:0)
这将遍历整个Object并输出您需要的值
$execute = '{
"1": {
"ID_PP": ["1","2","3" ],
"detail": {
"price": 16.5,
"tax": null,
"shipment": null
}
},
"2": {
"ID_PP": ["6","7","8","9","10"],
"detail": {
"price": 14,
"tax": null,
"shipment": null
}
},
"3": {
"ID_PP": ["13","14"],
"detail": {
"price": 6,
"tax": null,
"shipment": null
}
}
}';
$return = json_decode($execute);
print_r($return);
foreach($return as $arrItem){
foreach($arrItem->ID_PP as $key=>$val){
echo $val.',';
}
}
结果
1,2,3,6,7,8,9,10,13,14,