我正试图找出如何根据比较筛选对象。
我得到一个像这样的物体,虽然有15个结果…
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
我需要过滤这个对象,所以只返回
[flights]
,其中[filed_departuretime]
在查询结果$time = strtotime($item['departure_time']);
的1小时内。通过这样做:
abs($object->filed_departuretime - $time) <= 3600)
希望这一切都是有意义的,谢谢你的帮助:)我真的不知道数组过滤器是如何工作的,所以这是我目前所拥有的…
//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,
);*/
最佳答案
数组过滤器接受一个数组并通过一个函数传递每个元素。它只保留函数返回真值的元素。例子:
//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;
});