在php中,如何在对象数组中查找最接近的上下值?
在4组数据的数组列表中有52条记录列表,我有一个clostest编号。循环执行13*4在php中,如何在对象数组中查找最接近的上下值?,php,arrays,Php,Arrays,在4组数据的数组列表中有52条记录列表,我有一个clostest编号。循环执行13*4 $k = 0; for ($i = 0; $i < count($arr); $i++) { while (each($cur)) $c = $cur[$i + 1] - $cur[$i]; for ($j = 0; $j < $c; $j++) {
$k = 0;
for ($i = 0; $i < count($arr); $i++) {
while (each($cur))
$c = $cur[$i + 1] - $cur[$i];
for ($j = 0; $j < $c; $j++) {
if ($sp <= $spresult[$i*$j] ) {
echo $spresult[$i*$j];
}
}
}
输出:?我只得到最近的,但我希望在每个集合中越来越低的最近
Array
(
[500x1400] => 20.88
[600x2800] => 141.74
[500x2800] => 95.01
[500x1450] => 22.56
[560x1450] => 30.25
[560x2800] => 120.45
[630x1460] => 41.29
[710x1450] => 54.27
[800x1450] => 71.37
[900x1430] => 89.86
[1000x1450] => 115.74
[1120x1450] => 140.88
[1250x1450] => 176.19
)
这是我的@arr列表:
[20] => stdClass Object
(
[FAN_DIA] => 560
[FAN_RPM] => 2800
[FAN_CMH] => 1000
[FAN_SERIES_NAME] => AFPV3G
[BLADE_ANGLE_VALUE] => 25.00
[FAN_DETAIL_ID] => 74
[BLADE_ANGLE_ID] => 1
[LOWER_LIMIT] => 0
[UPPER_LIMIT] => 4849
[COFFSET_ID] => 1
[outlet_velocity] => 1.128370668862
[velocity_pressure] => 0.077872805281231
[total_eff] => 0.12516662291067
[static_eff] => 0.1250857524154
[STATIC_PRESSURE] => 120.45
[BKW] => 2.6223644246016
)
[21] => stdClass Object
(
[FAN_DIA] => 560
[FAN_RPM] => 2800
[FAN_CMH] => 1000
[FAN_SERIES_NAME] => AFPV3G
[BLADE_ANGLE_VALUE] => 26.00
[FAN_DETAIL_ID] => 74
[BLADE_ANGLE_ID] => 2
[LOWER_LIMIT] => 0
[UPPER_LIMIT] => 5069
[COFFSET_ID] => 2
[outlet_velocity] => 1.128370668862
[velocity_pressure] => 0.077872805281231
[total_eff] => 0.1156008258852
[static_eff] => 0.11552709102186
[STATIC_PRESSURE] => 122.01
[BKW] => 2.8761416269824
)
[22] => stdClass Object
(
[FAN_DIA] => 560
[FAN_RPM] => 2800
[FAN_CMH] => 1000
[FAN_SERIES_NAME] => AFPV3G
[BLADE_ANGLE_VALUE] => 27.00
[FAN_DETAIL_ID] => 74
[BLADE_ANGLE_ID] => 3
[LOWER_LIMIT] => 0
[UPPER_LIMIT] => 5288
[COFFSET_ID] => 3
[outlet_velocity] => 1.128370668862
[velocity_pressure] => 0.077872805281231
[total_eff] => 0.10705967318223
[static_eff] => 0.10699222066151
[STATIC_PRESSURE] => 123.52
[BKW] => 3.1440175628288
)
[23] => stdClass Object
(
[FAN_DIA] => 560
[FAN_RPM] => 2800
[FAN_CMH] => 1000
[FAN_SERIES_NAME] => AFPV3G
[BLADE_ANGLE_VALUE] => 28.00
[FAN_DETAIL_ID] => 74
[BLADE_ANGLE_ID] => 4
[LOWER_LIMIT] => 0
[UPPER_LIMIT] => 5508
[COFFSET_ID] => 4
[outlet_velocity] => 1.128370668862
[velocity_pressure] => 0.077872805281231
[total_eff] => 0.099894445192514
[static_eff] => 0.099832288530857
[STATIC_PRESSURE] => 125.07
[BKW] => 3.4118934986752
)
在这个集合中,当我给出输入sP=121时,120.45和122.01都应该出现在这里 我仍然有点困惑,但据我所知,您需要为每组找到最接近的sp。你可以试试这个。在这里,我创建了一个类,以便可以模拟数据的相关部分,因为它是一个类列表。我在代码中添加了注释
<?php
// Create the test data added some more items to the second set
$arr = array();
$arr[] = new obj(19.85, 500, 1400);
$arr[] = new obj(20.19, 500, 1400);
$arr[] = new obj(20.54, 500, 1400);
$arr[] = new obj(20.88, 500, 1400);
$arr[] = new obj(141.74,600, 2800);
$arr[] = new obj(82.74,600, 2800);
$arr[] = new obj(91.74,600, 2800);
$arr[] = new obj(101.74,600, 2800);
// End Test data
$input = 20; // Closest to this is what you want to find
/* 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 ($arr 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 - $input));
if (!isset($diff[$idx]) || (isset($diff[$idx]) && $tdiff[$idx]<$diff[$idx])){
$closest[$idx] = $value->STATIC_PRESSURE;
$diff[$idx] = $tdiff[$idx];
}
}
/* Code to find closest End */
print_r($closest);
/*
* A class to create the test data with relevant items
* This is needed for me to test as i don't have access to your data
* which is array of objects
*/
class obj{
var $STATIC_PRESSURE=0;
var $FAN_DIA;
var $FAN_RPM;
function __construct($sp, $dia, $rpm){
$this->STATIC_PRESSURE = $sp;
$this->FAN_DIA=$dia;
$this->FAN_RPM=$rpm;
}
}
编辑:这里是找到最接近的较大和较小sp的方法。我还没有测试过这个
$closest = array(); // Array to store closest of each set
foreach ($arr as $value) {
$idx = "{$value->FAN_DIA}x{$value->FAN_RPM}";
if ($value->STATIC_PRESSURE > $input){
if(!isset($closest[$idx]['big'])){
$closest[$idx]['big'] = $value->STATIC_PRESSURE;
}else{
if(($value->STATIC_PRESSURE - $input) < ($closest[$idx]['big'] - $input)){
$closest[$idx]['big'] = $value->STATIC_PRESSURE;
}
}
}else{
if(!isset($closest[$idx]['small'])){
$closest[$idx]['small'] = $value->STATIC_PRESSURE;
}else{
if(($input - $value->STATIC_PRESSURE) < ($input - $closest[$idx]['small'])){
$closest[$idx]['small'] = $value->STATIC_PRESSURE;
}
}
}
}
我没有正确理解这个问题。你能提供你的数组样本和你想找到的吗?从注释中读取代码太难了,请在你的问题中添加编辑。这次我已经做了。所以你们想在这里找到什么,我要比较输入静压和这个数据数组列表,找到clostest。取决于直径、叶片角度、转速。请帮助我。。我在这个问题上已经两天了,仍然很困惑。你的意思是,你必须从这个列表中找到最接近的静压,比如,如果你输入20,你应该找到第一个静压为19.85的物体?谢谢。你给出的任何解决方案都有效。。但我想在每一套都有最上面的和最下面的请帮助我。请帮帮我请帮帮我。。我在等你的答复,因为我开车迟到了。你是指集合中最大的数字和最小的sp吗?我已经编辑了$arr see n check out。。班斯。。我在等你的回答请帮我
Array
(
[500x1400] => 19.85
[600x2800] => 82.74
)
$closest = array(); // Array to store closest of each set
foreach ($arr as $value) {
$idx = "{$value->FAN_DIA}x{$value->FAN_RPM}";
if ($value->STATIC_PRESSURE > $input){
if(!isset($closest[$idx]['big'])){
$closest[$idx]['big'] = $value->STATIC_PRESSURE;
}else{
if(($value->STATIC_PRESSURE - $input) < ($closest[$idx]['big'] - $input)){
$closest[$idx]['big'] = $value->STATIC_PRESSURE;
}
}
}else{
if(!isset($closest[$idx]['small'])){
$closest[$idx]['small'] = $value->STATIC_PRESSURE;
}else{
if(($input - $value->STATIC_PRESSURE) < ($input - $closest[$idx]['small'])){
$closest[$idx]['small'] = $value->STATIC_PRESSURE;
}
}
}
}