如何在php中找到对象数组中较低和较高的最近值?

时间:2014-11-10 05:40:30

标签: php arrays

有52个记录列表,我喜欢在4组数据的数组列表中的最新数量。(循环执行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];
                    }


                }
            }

数据:

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
    )

输出:?我只是最接近,但我希望每组中的较低和最近。

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
    )

此集合中120.45122.01两者都应该在我输入sP=121;

时来到这里

1 个答案:

答案 0 :(得分:0)

我仍然有点困惑,但据我了解你需要为每一组找到最接近的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;
    }
}

输出:

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

编辑:这是找出最近和更大的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;
            }
        }
    }
}