Php array_filter if time is within 1 hour
I am trying to figure out how to filter an object based on a comparison.
I get an object that looks something like this, albeit with 15 results ....
stdClass Object
(
[FlightInfoExResult] => stdClass Object
(
[next_offset] => 15
[flights] => Array
(
[0] => stdClass Object
(
[faFlightID] => SWA2078-1499232401-airline-0885
[ident] => SWA2078
[aircrafttype] => B738
[filed_ete] => 03:00:00
[filed_time] => 1499232401
[filed_departuretime] => 1499477700
[filed_airspeed_kts] => 423
[filed_airspeed_mach] =>
[filed_altitude] => 0
[route] =>
[actualdeparturetime] => 0
[estimatedarrivaltime] => 1499489100
[actualarrivaltime] => 0
[diverted] =>
[origin] => KPHX
[destination] => KMKE
[originName] => Phoenix Sky Harbor Intl
[originCity] => Phoenix, AZ
[destinationName] => General Mitchell Intl
I need to filter this object, so return [flights]
where [filed_departuretime]
is within 1 hour after the query result$time = strtotime($item['departure_time']);
by doing something like this:
abs($object->filed_departuretime - $time) <= 3600)
hope this all makes sense, thanks for your help :) I really don't know how array_filter works, so this is what I have so far ....
//set flight identity
$ident = $item['airline'].$item['flight_number'];
$date = date('Y-m-d H:i:s');
$time = strtotime($item['departure_time']);
//get flightaware results
$flightAwareResult = FlightInfoEx($ident);
/*$flightResult = array_filter(
$flightAwareResult,
);*/
source to share
An array filter takes an array and passes each element through a function. It only contains elements for which the function returned true. Example:
//set flight identity
$ident = $item['airline'].$item['flight_number'];
$date = date('Y-m-d H:i:s');
$time = strtotime($item['departure_time']);
//get flightaware results
$flightAwareResult = FlightInfoEx($ident);
$filtered_array = array_filter($flightAwareResult->FlightInfoExResult->flights, function ($value) use ($time) {
return $time - $value->filed_departuretime) <= 3600;
});
source to share
Just try this
$object='';
$object=json_decode(json_encode($object,true),true);
$result=array_map(function ($array){
$time = strtotime($array['departure_time']);
if(($array['filed_departuretime'] - $time) <= 3600){
return $array;
}
},$object['FlightInfoExResult']['flights']);
$result=array_filter($result);
Instead, $object
enter the name of the object variable.
I will help you.
source to share