php - How to handle Objects with arrays to access specific data? -


i'm new working objects in php. have json object print_r output looks (please see below). i've edited shorten it, structure of it:

stdclass object (     [station_2char] => st     [stationname] => summit     [items] => stdclass object         (             [item] => array                 (                     [0] => stdclass object                         (                             [item_index] => 0                             [sched_dep_date] => 07:55:00 08/02/2013                             [destination] => dover                             [track] => 1                             [line] => m&e                             [train_id] => 6607                             [status] => aboard                             [sec_late] => 322                             [backcolor] => green                             [forecolor] => white                             [shadowcolor] => black                             [gpslatitude] =>                              [gpslongitude] =>                              [gpstime] => 8/2/2013 7:59:37                             [train_line] => morris & essex line                             [station_position] => 1                             [lineabbreviation] => m&e                             [inlinemsg] =>                              [stops] => stdclass object                                 (                                     [stop] => array                                         (                                             [0] => stdclass object                                                 (                                                     [name] => chatham                                                     [time] => 8/2/2013 8:05:31                                                 )                                              [1] => stdclass object                                                 (                                                     [name] => madison                                                     [time] => 8/2/2013 8:08:43                                                 )                                              [2] => stdclass object                                                 (                                                     [name] => convent station                                                     [time] => 8/2/2013 8:12:56                                                 )                          ... etc                                         )                                  )                          )                      [1] => stdclass object                         (                             [item_index] => 1                             [sched_dep_date] => 08:07:00 08/02/2013                             [destination] => hoboken                             [track] => 2                             [line] => m&e                             [train_id] => 414                             [status] => in 8 min                             [sec_late] => 81                             [backcolor] => lightgreen                             [forecolor] => black                             [shadowcolor] => lightgreen                             [gpslatitude] => 40.6951                             [gpslongitude] => -74.4034                             [gpstime] => 8/2/2013 7:59:59                             [train_line] => gladstone branch                             [station_position] => 1                             [lineabbreviation] => m&e                             [inlinemsg] =>                              [stops] => stdclass object                                 (                                     [stop] => array                                         (                                             [0] => stdclass object                                                 (                                                     [name] => maplewood                                                     [time] => 8/2/2013 8:14:53                                                 )                                              [1] => stdclass object                                                 (                                                     [name] => south orange ... etc 

this type of php coding method have been using access data. i'm wondering if there better way, because becomes kind of complex having "foreach" loop through @ need. had impression objects in php able access data more directly.

here php code wrote. it's function , while has worked unit testing of far, have feeling there better way more elegant in programming. function has 2 nested foreach loops , 3 "if" statements. seems lot me find connection times trip.

// $trip object above data sample. function get_connection($trip,$final_desired_dest,$connection_time) {  foreach ($trip $st_key=>$st_ny_trip) {  $xfer_depart_time = $st_ny_trip->sched_dep_date;   if ($st_ny_trip->stops->stop instanceof stdclass) {     $st_ny_trip->stops->stop = array($st_ny_trip->stops->stop); }  foreach ($st_ny_trip->stops->stop $key=>$value) {      if ($value->name == $final_desired_dest ) {         $connect_raw = datetime::createfromformat("m/d/y g:i:s a", $connection_time);         $xfer_depart_raw = datetime::createfromformat("h:i:s m/d/y", $xfer_depart_time);         $final_raw = datetime::createfromformat("m/d/y g:i:s a", $value->time);          if ($xfer_depart_raw > $connect_raw) {             $xfer = $xfer_depart_raw->format("m/d/y g:i:s a");             $final = $final_raw->format("m/d/y g:i:s a");             return array ($xfer,$final);         } // if (second)     } // if (first) } } // foreach (first)     return array ("",""); } // function end 

i'm looking better method improve on this. might overlooking how make full use of objects , oo programming here, forward being enlighten. thanks!

a start using objects , arrays use objects somethings , arrays lists (0,1, many, lots) of somethings. oversimplification, gets things started. objects particularly useful when can answer question (does train stop in x?) code once , use often.

since have objects in json , showing print_r, assume able translate raw data whatever format want. format show has fields necessary data interchange, maybe superfluous when using in programming. recommend removing unhelpful fields/levels (the stop in stops, e.g.) , converting lists arrays, other items objects.

i brute-forced data structure i'd use below. allows code this:

    $oscheduledeparture = generate_scheduled_departure_1();     $ostation = generate_station();      print_r($oscheduledeparture->getconnection('madison', '3/4/2013 3:14:00 am'));     print_r($ostation->goestodestination('hoboken'));     print_r($ostation->getconnections('madison', '3/4/2013 3:14:00 am')); 

you should able write parser convert raw data similar. hope helps!


     class station {              public function goestodestination($destination) {                 $areturn = array();                 foreach ($this->items $oscheduleddeparture) {                     if ($oscheduleddeparture->goestostation($destination)) {                         $areturn[] = $oscheduleddeparture;                     }                 }                 return $areturn;             }              public function getconnections($destination, $connection_time) {                 $areturn = array();                 foreach ($this->items $oscheduleddeparture) {                     if ($oscheduleddeparture->goestostation($destination)) {                         $areturn[] = array(                             'sched_dep_date'    => $oscheduleddeparture->sched_dep_date,                             'destination'       => $oscheduleddeparture->destination,                             'connection'        => $oscheduleddeparture->getconnection($destination, $connection_time),                         );                     }                 }                 return $areturn;             }          }          function generate_station() {              $astation = array(                 station_2char   => 'st',                 stationname     => 'summit',             );              $ostation = new station();             foreach ($aitem $key=>$value) {                 $ostation->$key = $value;             }              $ostation->items = array(                 generate_scheduled_departure_0(),                 generate_scheduled_departure_1(),             );             return $ostation;         }          class scheduleddeparture {              public function getscheduleddeparturetime() {                 return $this->sched_dep_date;             }              public function goestostation($destination) {                 if ($this->destination == $destination) {                     return true;                 }                  // check stops                 foreach ($this->stops $stop) {                     if ($stop->name == $destination) {                         return true;                     }                 }                 return false;             }              public function getconnection($destination, $connection_time) {                 $connect_raw = datetime::createfromformat("m/d/y g:i:s a", $connection_time);                 $xfer_depart_raw = datetime::createfromformat("h:i:s m/d/y", $this->getscheduleddeparturetime());                  foreach ($this->stops $stop) {                     if ($stop->name == $destination) {                         $final_raw = datetime::createfromformat("m/d/y g:i:s a", $stop->time);                          if ($xfer_depart_raw > $connect_raw) {                             $xfer = $xfer_depart_raw->format("m/d/y g:i:s a");                             $final = $final_raw->format("m/d/y g:i:s a");                             return array ($xfer,$final);                         }                      }                  }                  // no connection available                  return false;             }         }          function generate_scheduled_departure_0() {             $aitem = array(                 'item_index'        => '0',                 'sched_dep_date'    => '07:55:00 08/02/2013',                 'destination'       => 'dover',                 'track'             => '1',                 'line'              => 'm&e',                 'train_id'          => '6607',                 'status'            => 'all aboard',                 'sec_late'          => '322',                 'backcolor'         => 'green',                 'forecolor'         => 'white',                 'shadowcolor'       => 'black',                 'gpslatitude'       => '',                 'gpslongitude'      => '',                 'gpstime'           => '8/2/2013 7:59:37 am',                 'train_line'        => 'morris & essex line',                 'station_position'  => '1',                 'lineabbreviation'  => 'm&e',                 'inlinemsg'         => '',             );              $oscheduledeparture = new scheduleddeparture();             foreach ($aitem $key=>$value) {                 $oscheduledeparture->$key = $value;             }              $oscheduledeparture->stops = generate_scheduled_departure_0_stops();             return $oscheduledeparture;         }          function generate_scheduled_departure_0_stops() {              $astops = array();              //              $astop = array(                 'name' => 'chatham',                 'time' => '8/2/2013 8:05:31 am',             );             $astops[] = (object)$astop;              $astop = array(                 'name' => 'madison',                 'time' => '8/2/2013 8:08:43 am',             );             $astops[] = (object)$astop;              $astop = array(                 'name' => 'convent station',                 'time' => '8/2/2013 8:12:56 am',             );             $astops[] = (object)$astop;              return $astops;         }          // item 1         function generate_scheduled_departure_1() {             $aitem = array(                 'item_index'        => '1',                 'sched_dep_date'    => '08:07:00 08/02/2013',                 'destination'       => 'hoboken',                 'track'             => '2',                 'line'              => 'm&e',                 'train_id'          => '414',                 'status'            => 'in 8 min',                 'sec_late'          => '81',                 'backcolor'         => 'lightgreen',                 'forecolor'         => 'black',                 'shadowcolor'       => 'lightgreen',                 'gpslatitude'       => '40.6951',                 'gpslongitude'      => '-74.4034',                 'gpstime'           => '8/2/2013 7:59:59 am',                 'train_line'        => 'gladstone branch',                 'station_position'  => '1',                 'lineabbreviation'  => 'm&e',                 'inlinemsg'         => '',             );              $oscheduledeparture = new scheduleddeparture();             foreach ($aitem $key=>$value) {                 $oscheduledeparture->$key = $value;             }              $oscheduledeparture->stops = generate_scheduled_departure_1_stops();             return $oscheduledeparture;         }          function generate_scheduled_departure_1_stops() {              $astops = array();              $astop = array(                 'name' => 'maplewood',                 'time' => '8/2/2013 8:14:53 am',             );             $astops[] = (object)$astop;              $astop = array(                 'name' => 'south orange',                 'time' => '8/2/2013 8:08:43 am',             );             $astops[] = (object)$astop;              $astop = array(                 'name' => 'convent station',                 'time' => '8/2/2013 8:14:25 am',             );             $astops[] = (object)$astop;              return $astops;         } 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -