简体   繁体   English

如何在php中的对象数组中查找最近的值(上下限)?

[英]how to find nearest values both lower and upper in array of objects in php?

there is 52 records list which i have fond clostest number in array list of 4 sets of data.(loop executes 13*4 ) 在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++) {

                    if ($sp <= $spresult[$i*$j] ) {

                        echo $spresult[$i*$j];
                    }


                }
            }

Data: 数据:

Array
(
[0] => stdClass Object
    (
        [FAN_DIA] => 500
        [FAN_RPM] => 1400
        [FAN_CMH] => 1000
        [FAN_SERIES_NAME] => AFPV3G
        [BLADE_ANGLE_VALUE] => 25.00
        [FAN_DETAIL_ID] => 66
        [BLADE_ANGLE_ID] => 1
        [LOWER_LIMIT] => 0
        [UPPER_LIMIT] => 4849
        [COFFSET_ID] => 1
        [outlet_velocity] => 1.4154281670205
        [velocity_pressure] => 0.12253436672753
        [total_eff] => 0.30392346451101
        [static_eff] => 0.30205922285853
        [STATIC_PRESSURE] => 19.85
        [BKW] => 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
    )

output: ?I am getting only closest but i want in each set lower and lower nearest . 输出:?我只是越来越接近,但我想在每个集合中越来越低。

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
)

this is my @arr list: 这是我的@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
    )

here in this set 120.45 and 122.01 both should come here when i give input sP=121; 当我给定输入sP=121;时,这组中的120.45122.01都应该来到这里sP=121;

I am still a bit confused, but as i understand you need to find closest sp for each set. 我仍然有些困惑,但是据我了解,您需要为每组找到最接近的sp。 You can try this. 你可以试试看 Here I have created a class so that I can emulate the relevant parts of your data as it is a list of class. 在这里,我创建了一个类,以便可以模拟数据的相关部分,因为它是类的列表。 I have added comment in the code. 我在代码中添加了注释。

<?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;
    }
}

Output: 输出:

Array
(
    [500x1400] => 19.85
    [600x2800] => 82.74
)

Edit: Here is the method to find out the closest bigger and smaller sp. 编辑:这是找出最接近的较大和较小sp的方法。 I have not tested this. 我还没有测试。

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

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

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