我正在尝试在PHP中执行过滤功能。我已经开始做吼了:
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")
}
}
}
}
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"
}
}
我想要做的是在代码中打印为注释。我想做一个自定义的过滤器函数,它循环遍历一个看起来像我上面写的var_dump的对象数组,并返回带有最高“StopTime”,“higest NoSamples”或两者的数组实例(如果可能,否则只返回最大的“NoSamples”。
您将如何实施此过滤功能?在我的情况下是否准备好使用任何已完成的过滤器函数,它需要一组数组并对其执行过滤?
提前多多谢谢。
答案 0 :(得分:0)
假设$ 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];