mholm59 Posted May 30, 2007 Share Posted May 30, 2007 I am developing a site that deals with flight status information for the airline industry. Here's a sample of a query result from my data provider: stdClass Object ( [next_offset] => 1 [flights] => Array ( [0] => stdClass Object ( [ident] => UAL102 [aircrafttype] => B735 [filed_ete] => 03:05:00 [filed_time] => 1180442256 [filed_departuretime] => 1180456920 [filed_airspeed_kts] => 431 [filed_airspeed_mach] => [filed_altitude] => 350 [route] => PLAIN4 GLD J80 MCI J24 STL J8 IIU PSK SBV4 [actualdeparturetime] => 1180458000 [estimatedarrivaltime] => 1180469580 [actualarrivaltime] => 0 [diverted] => [origin] => KDEN [destination] => KRDU [originName] => Denver Int'l [originCity] => Denver, CO [destinationName] => Raleigh Durham Int'l [destinationCity] => Raleigh, NC ) ) ) I'm trying to use foreach in PHP to get into the subarray in the data... foreach ($results as $name => $number) { echo "Number for $name: $number\n"; foreach ($results as $key => $value) { echo "$key: $value\n"; } } The only result I'm getting is the next offset value. I can't get any of the data from the flight. It looks like the actual flight information is in a subarray. Does anyone have a sample PHP file that solves this or gets at the actual flight information in a different way? Many thank-yous in advance... Marc Quote Link to comment Share on other sites More sharing options...
chigley Posted May 30, 2007 Share Posted May 30, 2007 To make life easier for us all, print an example array like this please: <?php echo "<pre>".print_r($array,true)."</pre>"; ?> And put it in [pre ] BBCode tags Quote Link to comment Share on other sites More sharing options...
Barand Posted May 30, 2007 Share Posted May 30, 2007 It makes it even easier for us if you use var_export($array); and post the output. Quote Link to comment Share on other sites More sharing options...
mholm59 Posted May 30, 2007 Author Share Posted May 30, 2007 Sorry guys...here it is using var_export: stdClass::__set_state(array( 'next_offset' => 1, 'flights' => array ( 0 => stdClass::__set_state(array( 'ident' => 'UAL102', 'aircrafttype' => 'B733', 'filed_ete' => '02:14:00', 'filed_time' => 1180517323, 'filed_departuretime' => 1180531800, 'filed_airspeed_kts' => 435, 'filed_airspeed_mach' => '', 'filed_altitude' => 350, 'route' => 'LOUPE1 LIN PEONS INSLO DTA J199 EKR TOMSN4', 'actualdeparturetime' => 0, 'estimatedarrivaltime' => 1180539840, 'actualarrivaltime' => 0, 'diverted' => '', 'origin' => 'KSJC', 'destination' => 'KDEN', 'originName' => 'Norman Y Mineta San Jose Int\'l', 'originCity' => 'San Jose, CA', 'destinationName' => 'Denver Int\'l', 'destinationCity' => 'Denver, CO', )), ), )) Thanks again for your help! Quote Link to comment Share on other sites More sharing options...
chigley Posted May 30, 2007 Share Posted May 30, 2007 When you use var_export, view the source of the output and put it between [pre ] tags when posting in the forum. This way it keeps the formatting so we can read it! Quote Link to comment Share on other sites More sharing options...
Barand Posted May 30, 2007 Share Posted May 30, 2007 The advantage of var_export is that it can be inserted directly into a script for testing. With print_r it needs extensive editing to remove [ 's etc and add quotes. Just put it in a script and print_r it if you want it pretty (you'll have to remove the "stdClass::__set_state" Quote Link to comment Share on other sites More sharing options...
Barand Posted May 30, 2007 Share Posted May 30, 2007 <?php $data = array( 'next_offset' => 1, 'flights' => array ( 0 => (array( 'ident' => 'UAL102', 'aircrafttype' => 'B733', 'filed_ete' => '02:14:00', 'filed_time' => 1180517323, 'filed_departuretime' => 1180531800, 'filed_airspeed_kts' => 435, 'filed_airspeed_mach' => '', 'filed_altitude' => 350, 'route' => 'LOUPE1 LIN PEONS INSLO DTA J199 EKR TOMSN4', 'actualdeparturetime' => 0, 'estimatedarrivaltime' => 1180539840, 'actualarrivaltime' => 0, 'diverted' => '', 'origin' => 'KSJC', 'destination' => 'KDEN', 'originName' => 'Norman Y Mineta San Jose Int\'l', 'originCity' => 'San Jose, CA', 'destinationName' => 'Denver Int\'l', 'destinationCity' => 'Denver, CO', )), ), ); echo '<pre>', print_r($data, true), '</pre>'; // to get ident of first flight, index 0, then $ident = $data['flights'][0]['ident']; echo $ident; // UAL102 ?> Quote Link to comment Share on other sites More sharing options...
mholm59 Posted May 30, 2007 Author Share Posted May 30, 2007 Here it is using print_r with pre tags around it. Sorry about the confusion with the formatting! :-) Barand -- I appreciate your help with the sample code you gave. It looks like you edited the output I was given by my data provider, tho. Will that sample code you posted work with the data as given below? What you see below is how I get it from my provider. I need something where I don't have to edit out the "stdClass Object" at the top, for example. [pre] stdClass Object ( [next_offset] => 1 [flights] => Array ( [0] => stdClass Object ( [ident] => UAL102 [aircrafttype] => B733 [filed_ete] => 03:05:00 [filed_time] => 1180534118 [filed_departuretime] => 1180547880 [filed_airspeed_kts] => 439 [filed_airspeed_mach] => [filed_altitude] => 350 [route] => PLAIN4 MCK J130 PWE IRK SPI J80 VHP J24 FLM BKW ROA SBV4 [actualdeparturetime] => 0 [estimatedarrivaltime] => 1180558980 [actualarrivaltime] => 0 [diverted] => [origin] => KDEN [destination] => KRDU [originName] => Denver Int'l [originCity] => Denver, CO [destinationName] => Raleigh Durham Int'l [destinationCity] => Raleigh, NC ) ) ) [/pre] Quote Link to comment Share on other sites More sharing options...
mholm59 Posted May 30, 2007 Author Share Posted May 30, 2007 Here is the same data using var_export and using pre tags: [pre] stdClass::__set_state(array( 'next_offset' => 1, 'flights' => array ( 0 => stdClass::__set_state(array( 'ident' => 'UAL102', 'aircrafttype' => 'B733', 'filed_ete' => '03:05:00', 'filed_time' => 1180534118, 'filed_departuretime' => 1180547880, 'filed_airspeed_kts' => 439, 'filed_airspeed_mach' => '', 'filed_altitude' => 350, 'route' => 'PLAIN4 MCK J130 PWE IRK SPI J80 VHP J24 FLM BKW ROA SBV4', 'actualdeparturetime' => 0, 'estimatedarrivaltime' => 1180558980, 'actualarrivaltime' => 0, 'diverted' => '', 'origin' => 'KDEN', 'destination' => 'KRDU', 'originName' => 'Denver Int\'l', 'originCity' => 'Denver, CO', 'destinationName' => 'Raleigh Durham Int\'l', 'destinationCity' => 'Raleigh, NC', )), ), )) [/pre] Quote Link to comment Share on other sites More sharing options...
Barand Posted May 30, 2007 Share Posted May 30, 2007 Trouble is, if I test anything with that in, I get Fatal error: Call to undefined method stdClass::__set_state() so can't help Quote Link to comment Share on other sites More sharing options...
mholm59 Posted May 30, 2007 Author Share Posted May 30, 2007 Is there a way to use substr, strpos, etc. to "strip out" the extra/unneeded areas? I was trying to do that and didn't have any luck, but I was just taking the data raw as its given by the provider. I wonder if I used something like print_r or var_export if it would let me take data out before putting it back into an array. Any ideas if that would work? Quote Link to comment Share on other sites More sharing options...
Barand Posted May 30, 2007 Share Posted May 30, 2007 If I substute xtdClass for stdClass and define xtdClass, it works <?php class xtdClass { public static function __set_state($array) { return $array; } } $data = xtdClass::__set_state(array( 'next_offset' => 1, 'flights' => array ( 0 => xtdClass::__set_state(array( 'ident' => 'UAL102', 'aircrafttype' => 'B733', 'filed_ete' => '03:05:00', 'filed_time' => 1180534118, 'filed_departuretime' => 1180547880, 'filed_airspeed_kts' => 439, 'filed_airspeed_mach' => '', 'filed_altitude' => 350, 'route' => 'PLAIN4 MCK J130 PWE IRK SPI J80 VHP J24 FLM BKW ROA SBV4', 'actualdeparturetime' => 0, 'estimatedarrivaltime' => 1180558980, 'actualarrivaltime' => 0, 'diverted' => '', 'origin' => 'KDEN', 'destination' => 'KRDU', 'originName' => 'Denver Int\'l', 'originCity' => 'Denver, CO', 'destinationName' => 'Raleigh Durham Int\'l', 'destinationCity' => 'Raleigh, NC', )), ), )); echo '<pre>', print_r($data, true), '</pre>'; ?> I get the impression from the manual that it should be defined since v5.1.0, and I'm using 5.1.2 ??? Quote Link to comment Share on other sites More sharing options...
mholm59 Posted May 30, 2007 Author Share Posted May 30, 2007 Getting closer... The array coming from my provider is in a variable called $results. When I do a print_r on $results, this is what I get: [pre] stdClass Object ( [next_offset] => 1 [flights] => Array ( [0] => stdClass Object ( [ident] => UAL102 [aircrafttype] => B733 [filed_ete] => 03:05:00 [filed_time] => 1180534118 [filed_departuretime] => 1180547880 [filed_airspeed_kts] => 439 [filed_airspeed_mach] => [filed_altitude] => 350 [route] => PLAIN4 MCK J130 PWE IRK SPI J80 VHP J24 FLM BKW ROA SBV4 [actualdeparturetime] => 1180546800 [estimatedarrivaltime] => 1180558080 [actualarrivaltime] => 0 [diverted] => [origin] => KDEN [destination] => KRDU [originName] => Denver Int'l [originCity] => Denver, CO [destinationName] => Raleigh Durham Int'l [destinationCity] => Raleigh, NC ) ) ) [/pre] Here is the code I'm using: echo "<P><hr size=1>new attempt:</P>"; class xtdClass { public static function __set_state($results) { return $results; } } $data = xtdClass::__set_state(array($results)); echo '<pre>', print_r($results, true), '</pre>'; echo "<P><hr size=1>Attempt at output:</P>"; $ident = $results['flights'][0]['ident']; $actype = $results['flights'][0]['aircrafttype']; echo $ident . "<BR>" . $actype; // UAL102 and B733 I'm getting this error: Fatal error: Cannot use object of type stdClass as array in /home/onevhol9/public_html/flightbet/data/flightaware_dataget_flightinfo.php on line 36 Quote Link to comment Share on other sites More sharing options...
Barand Posted May 30, 2007 Share Posted May 30, 2007 try echo $results->flights[0]->ident; Quote Link to comment Share on other sites More sharing options...
mholm59 Posted May 30, 2007 Author Share Posted May 30, 2007 That did it! You totally rock! Thank you so much!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.