doujia1988 2017-07-06 14:25
浏览 96

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

I am trying to figure out how to filter an object based on a comparison.

I get an object that looks something like this, although 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 only return the [flights] where [filed_departuretime] is within 1 hour of Query result $time = strtotime($item['departure_time']);

by doing something like this:

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

hopefully this all makes sense, thanks for your help :) I dont really 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,
);*/
  • 写回答

2条回答 默认 最新

  • dongshi3605 2017-07-06 14:34
    关注

    Array filter takes an array and passes each element through a function. It only keeps elements for which the function returned a true value. 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; 
    }); 
    
    评论

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch