如何在php中找到对象数组中较低和较高的最近值?
/* Code to find closest Start */
$closest = array(); // Array to store closest of each set
$diff = array(); //Array to store diffs to calculate the closest
foreach ($array as $value) {
// I am using DIAxRPM as key like 500x1400 for the first set
$idx = "{$value->FAN_DIA}x{$value->FAN_RPM}";
$tdiff[$idx] = abs(($value->STATIC_PRESSURE - $sp));
//echo $tdiff[$idx]." " ;
if (!isset($diff[$idx]) || (isset($diff[$idx]) && $tdiff[$idx]<$diff[$idx] ) ){
$closest[$idx] = $value->STATIC_PRESSURE;
$diff[$idx] = $tdiff[$idx];
echo $diff[$idx]."";
}
}
print_r($closest);
输出:
Array
(
[500x1400] => 19.85
[600x2800] => 141.74
[500x2800] => 91.10
[500x1450] => 21.46
[560x1450] => 28.88
[560x2800] => 120.45
[630x1460] => 39.61
[710x1450] => 52.29
[800x1450] => 68.89
[900x1430] => 86.69
[1000x1450] => 111.46
[1120x1450] => 140.88
[1250x1450] => 176.19
)
这里我得到的最接近,但我想要数组对象中的较低和最近的值。请帮助我在过去2天内遇到此问题..
这是我的$数组 排列 ( [0] =&gt; stdClass对象 ( [FAN_DIA] =&gt; 500 [FAN_RPM] =&gt; 1400 [FAN_CMH] =&gt; 1000 [FAN_SERIES_NAME] =&gt; AFPV3G [BLADE_ANGLE_VALUE] =&gt; 25.00 [FAN_DETAIL_ID] =&gt; 66 [BLADE_ANGLE_ID] =&gt; 1 [LOWER_LIMIT] =&gt; 0 [UPPER_LIMIT] =&gt; 4849 [COFFSET_ID] =&gt; 1 [outlet_velocity] =&gt; 1.4154281670205 [velocity_pressure] =&gt; 0.12253436672753 [total_eff] =&gt; 0.30392346451101 [static_eff] =&gt; 0.30205922285853 [STATIC_PRESSURE] =&gt; 19.85 [BKW] =&gt; 0.179 )
[1] => stdClass Object
(
[FAN_DIA] => 500
[FAN_RPM] => 1400
[FAN_CMH] => 1000
[FAN_SERIES_NAME] => AFPV3G
[BLADE_ANGLE_VALUE] => 26.00
[FAN_DETAIL_ID] => 66
[BLADE_ANGLE_ID] => 2
[LOWER_LIMIT] => 0
[UPPER_LIMIT] => 5069
[COFFSET_ID] => 2
[outlet_velocity] => 1.4154281670205
[velocity_pressure] => 0.12253436672753
[total_eff] => 0.28229305022887
[static_eff] => 0.28059050300543
[STATIC_PRESSURE] => 20.19
[BKW] => 0.196
)
[2] => stdClass Object
(
[FAN_DIA] => 500
[FAN_RPM] => 1400
[FAN_CMH] => 1000
[FAN_SERIES_NAME] => AFPV3G
[BLADE_ANGLE_VALUE] => 27.00
[FAN_DETAIL_ID] => 66
[BLADE_ANGLE_ID] => 3
[LOWER_LIMIT] => 0
[UPPER_LIMIT] => 5288
[COFFSET_ID] => 3
[outlet_velocity] => 1.4154281670205
[velocity_pressure] => 0.12253436672753
[total_eff] => 0.26792548210575
[static_eff] => 0.26633643803054
[STATIC_PRESSURE] => 20.54
[BKW] => 0.21
)
[3] => stdClass Object
(
[FAN_DIA] => 500
[FAN_RPM] => 1400
[FAN_CMH] => 1000
[FAN_SERIES_NAME] => AFPV3G
[BLADE_ANGLE_VALUE] => 28.00
[FAN_DETAIL_ID] => 66
[BLADE_ANGLE_ID] => 4
[LOWER_LIMIT] => 0
[UPPER_LIMIT] => 5508
[COFFSET_ID] => 4
[outlet_velocity] => 1.4154281670205
[velocity_pressure] => 0.12253436672753
[total_eff] => 0.25302701773787
[static_eff] => 0.25155047235825
[STATIC_PRESSURE] => 20.88
[BKW] => 0.226
)
[4] => stdClass Object
(
[FAN_DIA] => 600
[FAN_RPM] => 2800
[FAN_CMH] => 1000
[FAN_SERIES_NAME] => AFPV3G
[BLADE_ANGLE_VALUE] => 25.00
[FAN_DETAIL_ID] => 68
[BLADE_ANGLE_ID] => 1
[LOWER_LIMIT] => 0
[UPPER_LIMIT] => 4849
[COFFSET_ID] => 1
[outlet_velocity] => 0.98293622709759
[velocity_pressure] => 0.05909257654684
[total_eff] => 0.10373541003493
[static_eff] => 0.10369217931877
[STATIC_PRESSURE] => 141.74
[BKW] => 3.72252672
)
[5] => stdClass Object
(
[FAN_DIA] => 600
[FAN_RPM] => 2800
[FAN_CMH] => 1000
[FAN_SERIES_NAME] => AFPV3G
[BLADE_ANGLE_VALUE] => 26.00
[FAN_DETAIL_ID] => 68
[BLADE_ANGLE_ID] => 2
[LOWER_LIMIT] => 0
[UPPER_LIMIT] => 5069
[COFFSET_ID] => 2
[outlet_velocity] => 0.98293622709759
[velocity_pressure] => 0.05909257654684
[total_eff] => 0.094872284113262
[static_eff] => 0.094833230277887
[STATIC_PRESSURE] => 143.49
[BKW] => 4.12065792
)
[6] => stdClass Object
(
[FAN_DIA] => 600
[FAN_RPM] => 2800
[FAN_CMH] => 1000
[FAN_SERIES_NAME] => AFPV3G
[BLADE_ANGLE_VALUE] => 27.00
[FAN_DETAIL_ID] => 68
[BLADE_ANGLE_ID] => 3
[LOWER_LIMIT] => 0
[UPPER_LIMIT] => 5288
[COFFSET_ID] => 3
[outlet_velocity] => 0.98293622709759
[velocity_pressure] => 0.05909257654684
[total_eff] => 0.088309379039201
[static_eff] => 0.088273449510657
[STATIC_PRESSURE] => 145.18
[BKW] => 4.478976
)
答案 0 :(得分:0)
我不确定我的答案是否正确,但你可以: