简体   繁体   English

PHP中的自定义过滤器数组值

[英]Customized Filter array values in php

I'm trying to do a filter function in PHP. 我正在尝试在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: 然后var_dump打印出波纹管:

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". 我想做一个自定义的过滤器函数,该函数遍历对象的数组类似于我上面编写的var_dump,并返回带有Higest的“ StopTime”,“ higest NoSamples”或两者的数组实例(如果可能,否则仅返回最大的“ 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. 在此先感谢。

Assuming the $arrays variable contains your var_dump data: 假设$ arrays变量包含您的var_dump数据:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM