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
                                                )

                                        )

                                )

                        )

                )

    Looks like location is an array, so you might have to use something like $service->location[0]->locationName, perhaps?

      Hi NogDog,

      This is what I did to make it work.

      `require("OpenLDBWS.php");
      $OpenLDBWS = new OpenLDBWS("MY TOKEN");
      $response = $OpenLDBWS->GetDepBoardWithDetails(1,"GLC");
      header("Content-Type: text/plain");

      //print_r ($response);

      if (isset($response->GetStationBoardResult->trainServices->service))
      {


       foreach($response->GetStationBoardResult->trainServices->service as $service)
      {
       
      $destinations = array();
      foreach($service->destination->location as $location)
      {
          $destinations[] = $location->locationName;
          $desticrs[] = $location->crs;
      }
        
      $recordtime = substr($response->GetStationBoardResult->generatedAt, 0, 16);
      $origin_location = $response->GetStationBoardResult->locationName;
      $origin_crs= $response->GetStationBoardResult->crs;
      $time = $service->std;
      $serviceID = $service->serviceID;
      $operator = $service->operator;
      $dest = implode($destinations);
      $dest_crs = implode($desticrs);
      $plat = $service->platform;
      $est= $service->etd;
      $datetime = substr($recordtime, 0, 16);
      $recordtime = str_replace('T',' ',$recordtime); 
      $recordtime = date("Y-m-d H:i",strtotime($recordtime));
      // Perform database insert

      }`

        Write a Reply...