I'm new to working with objects in PHP. I have a JSON object with print_r output that looks like this (please see below). I've edited it to shorten it, but this is the 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] => 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] =>
[STOPS] => stdClass Object
(
[STOP] => Array
(
[0] => stdClass Object
(
[NAME] => Chatham
[TIME] => 8/2/2013 8:05:31 AM
)
[1] => stdClass Object
(
[NAME] => Madison
[TIME] => 8/2/2013 8:08:43 AM
)
[2] => stdClass Object
(
[NAME] => Convent Station
[TIME] => 8/2/2013 8:12:56 AM
)
... 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 AM
[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 AM
)
[1] => stdClass Object
(
[NAME] => South Orange
... etc
This is the type of PHP coding method I have been using to access this data. I'm wondering if there is a better way, because it becomes kind of complex with having to "foreach" loop through it to get at what I need. I had the impression with Objects in PHP I would be able to access the data more directly.
Here is some PHP code I wrote. It's a function and while it has worked from my unit testing of it so far, I have the feeling there is a better way to do this to be more elegant in the programming. This function has two nested foreach loops and three "if" statements. That just seems like a lot to me to find the connection times for a trip.
// $trip is the Object from the above data sample.
function get_connection($trip,$final_desired_dest,$connection_time) {
foreach ($trip as $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 as $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 for a better method to improve on this. I might be greatly overlooking how to make full use of the Objects and OO programming here, so I look forward to being enlighten. Thanks!
A start to using objects and arrays is to use objects for somethings and arrays for lists (0,1, many, lots) of somethings. An oversimplification, but it gets things started. Objects are particularly useful when they can answer a question about themselves (does this train stop in X?) as you code it once and use it often.
Since you have your objects in JSON and were showing a print_r
, I assume that you are able to translate the raw data to whatever format you want. The format you show has some extra fields that are necessary for the data interchange, but maybe superfluous when using it in programming. I would recommend removing unhelpful fields/levels (the STOP in STOPS, e.g.) and converting lists to arrays, other items to objects.
I brute-forced the data into the structure I'd use below. It allows code like 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 be able to write a parser to convert your raw data into something similar. Hope it helps!
class Station { public function goesToDestination($destination) { $aReturn = array(); foreach ($this->ITEMS as $oScheduledDeparture) { if ($oScheduledDeparture->goesToStation($destination)) { $aReturn[] = $oScheduledDeparture; } } return $aReturn; } public function getConnections($destination, $connection_time) { $aReturn = array(); foreach ($this->ITEMS as $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 as $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 as $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 as $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 as $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 as $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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With