- Edited
Hi all,
I am trying to access elements of an array but I am having issues when it come to accessing elements which are in a sub array.
My code:
$OpenLDBWS = new OpenLDBWS("MY TOKEN");
$data = $OpenLDBWS->GetDepartureBoard(1,"GLC");
header("Content-Type: text/plain");
$results=$data->GetStationBoardResult;
/* The services... */
$service=$results->trainServices->service;
if( is_array( $service ) ){
/* Multiple records */
/* an array of objects */
foreach( $service as $index => $obj ){
$keys=array_keys( get_object_vars( $obj ) );
//print_r ($keys);
foreach( $keys as $key ){
${$key}=$obj->$key;
//print_r ($keys);
echo "Destination " .$service->location->locationName;
}
}
}
using this line " echo "Destination " .$service->location->locationName" I was expecting it to return the content of
"locationName".
Can anyone see where I am going wrong.
The array I am working on is:
stdClass Object
(
[GetStationBoardResult] => stdClass Object
(
[generatedAt] => 2021-07-09T09:19:32.4132656+01:00 // THIS DATA IS RETURNED
[locationName] => Glasgow Central // THIS DATA IS RETURNED
[crs] => GLC // THIS DATA IS RETURNED
[platformAvailable] => 1 // THIS DATA IS RETURNED
[trainServices] => stdClass Object
(
[service] => Array
(
[0] => stdClass Object
(
[std] => 09:18 // THIS DATA IS NOT RETURNED
[etd] => On time // THIS DATA IS NOT RETURNED
[platform] => 8R // THIS DATA IS NOT RETURNED
[operator] => ScotRail // THIS DATA IS NOT RETURNED
[operatorCode] => SR // THIS DATA IS NOT RETURNED
[serviceType] => train // THIS DATA IS NOT RETURNED
[serviceID] => I/U30cUsp5Jk+ANreHmh2Q== // THIS DATA IS NOT RETURNED
[rsid] => SR916200 // THIS DATA IS NOT RETURNED
[origin] => stdClass Object
(
[location] => Array
(
[0] => stdClass Object
(
[locationName] => Glasgow Central // THIS DATA IS NOT RETURNED
[crs] => GLC // THIS DATA IS NOT RETURNED
)
)
)
[destination] => stdClass Object
(
[location] => Array
(
[0] => stdClass Object
(
[locationName] => East Kilbride // THIS DATA IS NOT RETURNED
[crs] => EKL // THIS DATA IS NOT RETURNED
)
)
)
)
)