如果打开URL,您将看到它是一个很长的子对象字符串。我想提取70位置的值。到目前为止,我已经能够毫无问题地提取第一棵树了……但是,如果您深入研究,我将完全没有任何反馈。请检查下面的代码,并告诉我,我在做什么错了?
$url= "https://bwt.cbp.gov/api/waittimes";
$port = file_get_contents($url); // put the contents of the file into a variable
$data = json_decode($port); // decode the JSON feed
echo $data[69]->port_name.'<br>';
echo $data[69]->port_status.'<br>';
echo $data[69]->passenger_vehicle_lanes->maximum_lanes.'<br>';
echo $data[69]->passenger_vehicle_lanes->standard_lanes->lanes_open.'<br>';
答案 0 :(得分:1)
您能否尝试像这样更改rsyslog
(如果将true更改为数组,则最好将其访问数组)并像在数组json_decode($port, true);
中访问它一样
答案 1 :(得分:1)
以下内容对我有用:
$url= "https://bwt.cbp.gov/api/waittimes";
$port = file_get_contents($url); // put the contents of the file into a variable
$data = json_decode($port, true); // decode the JSON feed
echo "There are ".count($data)."Ports".PHP_EOL;
$found=false;
foreach ($data as $key => $value) {
//EDIT AFTER COMMENT**
if($value['port_number']==250401){
echo $value['port_name'].' '.$value['crossing_name'].PHP_EOL;
$found=true;
break;
}
}
if(!$found) echo "couldn't find port #";