1

If you open the URL, you'll see that it's a very long string of sub objects. I want to extract the values for the 70 position. So far I've been able to extract the first tree without a problem ... But if you go deeper then I don’t get any feedback at all. Please check the code below and tell me, what am I doing wrong?

$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>';
6
  • 1
    Hi, can you try to change json_decode($port, true); like this and access it like in array echo $data[69]['passenger_vehicle_lanes']['maximum_lanes'].'<br>'; Commented Jul 7, 2018 at 7:33
  • 1
    Can you post json data or object? Commented Jul 7, 2018 at 7:34
  • Your first suggestion worked perfectly. Boy am I glad you helped me. Commented Jul 7, 2018 at 7:41
  • I tried the code above (PHP 7.0.13) and I get the correct data "San Ysidro, Open, 24" from what I can see. There's no other code in the script that could interfere? Commented Jul 7, 2018 at 7:41
  • Well yes that's as far as it goes. The fourth line is not showing the results.. (lanes_open) Commented Jul 7, 2018 at 7:48

2 Answers 2

1

The following is working for me:

$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 #";
Sign up to request clarification or add additional context in comments.

6 Comments

Nice, but I was looking for a specific chunk of information within the string, I like your code tough, good job.
This was just an example on how to scan your data... About the specific part you're looking for at the 70th position... I would be careful about relying on the order provided by an external service... I recommend to find it through a unique ID, not by position....
Ok... now that sounds interesting... How would you go about it.. if<port_number>250401 then echo <port_name>and<crossing_name/>And<hours> (Not meant to be rude).. You really stirred my interest.
Since your answer doesnt fit for indexing on port_number the only solution I see is to scan the answer and return the wanted result... see my edit
I added also a check if nothing is found.... breaking the loop is ok if you're sure there's only one result.... otherwise you should continue and return the whole list of matching results...
|
1

can you try to change json_decode($port, true); (true will change object to array and it will be better to access it) like this and access it like in array echo $data[69]['passenger_vehicle_lanes']['maximum_lanes'].'<br>';

1 Comment

Excellent approach,it worked perfectly. Thank You Olexander, You rock.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.