gpt4 book ai didi

php array_filter 如果时间在 1 小时内

转载 作者:搜寻专家 更新时间:2023-10-31 21:50:24 25 4
gpt4 key购买 nike

我想弄清楚如何根据比较来过滤对象。

我得到一个看起来像这样的对象,尽管有 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']);

通过做这样的事情:

abs($object->filed_departuretime - $time) <= 3600)

希望这一切都有意义,感谢您的帮助 :) 我真的不知道 array_filter 是如何工作的,所以这就是我目前所知道的....

//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;
});

关于php array_filter 如果时间在 1 小时内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44951704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com