Jump to content

[SOLVED] PHP Arrays/Subarrays


mholm59

Recommended Posts

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...    ;D

Marc

Link to comment
https://forums.phpfreaks.com/topic/53559-solved-php-arrayssubarrays/
Share on other sites

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!

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"

<?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
?>

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]

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]

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?

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 ???

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.