如何在php中查找最接近的上下限值?

如何在php中查找最接近的上下限值?,php,arrays,Php,Arrays,在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

在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 大堆 ( [0]=>stdClass对象 ( [风扇直径]=>500 [风扇转速]=>1400 [FAN_CMH]=>1000 [FAN_系列_名称]=>AFPV3G [叶片角度值]=>25.00 [FAN_DETAIL_ID]=>66 [叶片角度ID]=>1 [下限]=>0 [上限]=>4849 [COFFSET_ID]=>1 [出口速度]=>1.4154281670205 [速度/压力]=>0.12253436672753 [总效率]=>0.30392346451101 [静态效率]=>0.30205922285853 [静压]=>19.85 [BKW]=>0.179 )


我不确定我是否正确理解了你的答案,但你能:

  • 使用k_排序对数组进行排序
  • 迭代数组,直到找到一个更大的元素,然后取上一个
  • 这两个元素就是您要搜索的元素

数组中最近的键或最近的值?数组中最近的值array@priyaa你能提供你想要的输出吗?公布你的阵列格式和值…如列表所示,第一个阵列直径500,rpm 1500,叶片角度为25.第二个直径500 rpm 1400 n角度为26.如i hv 4叶片角度25,26,27,28,相同直径和prm.再次f或直径550 rpm 2000 i具有25,26,27,28叶片角度…如列表所示,第一阵列直径500,rpm 1500,叶片角度为25.第二阵列直径500 rpm 1400 n角度为26.如i hv 4叶片角度25,26,27,28,对于相同直径和prm.同样,直径550 rpm 2000 i具有25,26,27,28叶片角度。。
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
    )
[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
    )