duanjue9889 2015-02-03 17:25
浏览 45
已采纳

php中的自定义Filter数组值

I'm trying to do a filter function in PHP. I have started doing the bellow:

function handleDuplicates($duplicateMonthReportsArray,$NodeReports,$whatToCompare){
    foreach ($duplicateMonthReportsArray as $duplicate) {
       $duplicateReportsArray = $NodeReports->nodeReports[$duplicate]->indvReports;
       var_dump($duplicateReportsArray); //Prints out what I have written bellow
       foreach ($duplicateReportsArray as $duplicateReport) {
           if($whatToCompare==="both"){
               //Return higest [NoSamples] and latest ["StopTime"] (If possible).
           }
           else if($whatToCompare==="latest"){
               //Return array with latest ["StopTime"]          
           }else{
              //Return array with higest [NoSamples] (If both same like the bellow case then return latest "StopTime")

           }
       }
    }
}

And the var_dump prints out the bellow:

array(2) {
  [""AU Feb-13",201302282                                                                                                                                                                                                            "]=>
  array(2) {
    ["StopTime"]=>
    string(23) "2013-02-28 23:00:00.000"
    ["NoSamples"]=>
    string(5) "673.0"
  }
  [""AU Feb-13",201302282                                                                                                                                                                                                            "]=>
  array(2) {
    ["StopTime"]=>
    string(23) "2013-02-28 23:55:00.000"
    ["NoSamples"]=>
    string(5) "673.0"
  }
}

What I want to do is printed in the code as comments. I want to do a customized filter function that loops through an array of objects that looks like the var_dump that I have written above and either return the array instance with higest "StopTime", "higest NoSamples" or both (If possible, otherwise return only the largest "NoSamples".

How would you go about implementing this filterfunction? Are there any finished filter functions ready to be used in my case, that takes an array of arrays and perform a filter on that?

Thanks alot in advance.

  • 写回答

1条回答 默认 最新

  • doufei4923 2015-02-03 17:35
    关注

    Assuming the $arrays variable contains your var_dump data:

    $maxStopTime = "";    
    $maxStopTimeIx = 0;
    
    $maxNoSamples = "";
    $maxNoSamplesIx = 0;
    
    foreach ($arrays as $k=>$array) {
       if ($array['StopTime'] > $maxStopTime) {
          $maxStopTimeIx = $k;
          $maxStopTime = $array['StopTime'];
       }
    
       if ($array['NoSamples'] > $maxNoSamples) {
          $maxNoSamplesIx = $k;
          $maxNoSamples = $array['NoSamples'];
       }
    }
    
    $maxStopTimeArray = $arrays[$maxStopTimeIx];
    $maxNoSamplesArray = $arrays[$maxNoSamplesIx];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址