0

Ive connected to an API named transportapi. Ive stored the results into a variable and decoded them to JSON. Im able to print out the first line of result but i wish to print them all out using some kind of loop.

here is my attempt

$url="https://transportapi.com/v3/uk/train/station/eus/2018-04-15/13:38/timetable.json?app_id=*****&app_key=*****&destination=LIV&train_status=passenger";

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);

curl_setopt($curl,CURLOPT_HEADER, 0);

$response = curl_exec($curl);

curl_close($curl);          

$datas = json_decode($response);
echo $datas[0]->station_name;

foreach ($datas as $data) {
    echo $data->station_name . '<br>';
}

?>

if i use echo $data->date; that only prints out the first result. but i need all of the results. Any help with the correct syntax would be much appreciated :)

Alex Howansky
  • 50,515
  • 8
  • 78
  • 98
  • 1
    Your url only returns one element. Your code works correctly. – gview Apr 13 '18 at 18:59
  • If you want departures, that is a subelement that has an array, but your question does not state that. – gview Apr 13 '18 at 19:00
  • Possible duplicate of [How can I parse a JSON file with PHP?](https://stackoverflow.com/questions/4343596/how-can-i-parse-a-json-file-with-php) – IncredibleHat Apr 13 '18 at 19:10
  • 4
    You've posted your app id and key to a public forum, you'll want to change and/or revoke them immediately. – Alex Howansky Apr 13 '18 at 19:16

0 Answers0