php array_filter如果时间在1小时之内

时间:2017-07-06 14:25:22

标签: php

我试图弄清楚如何根据比较过滤对象。

我得到一个看起来像这样的对象,虽然有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}}

的1小时内

做这样的事情:

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,
);*/

2 个答案:

答案 0 :(得分:0)

数组过滤器接受一个数组并通过函数传递每个元素。它只保留函数返回true值的元素。例如:

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

答案 1 :(得分:0)

试试这个

$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);

代替$object放置您的对象变量名称。

我会帮助你。