Php 与usort一起使用时组合函数?

Php 与usort一起使用时组合函数?,php,Php,我有一个包含天气值的数组。我现在有了一个可以工作的php脚本,可以根据温度对数组进行排序,然后提取温度的最大值,然后再次根据压力对数组进行排序,得到最大压力值 function compare_pressure($a, $b) { return strnatcmp($a['pressure'], $b['pressure']); } function compare_temp($a, $b) { return strnatcmp($a['temp'], $b['temp']);

我有一个包含天气值的数组。我现在有了一个可以工作的php脚本,可以根据温度对数组进行排序,然后提取温度的最大值,然后再次根据压力对数组进行排序,得到最大压力值

function compare_pressure($a, $b)
{
    return strnatcmp($a['pressure'], $b['pressure']);
}

function compare_temp($a, $b)
{
    return strnatcmp($a['temp'], $b['temp']);
}

usort($table, 'compare_pressure');
$mypress = $table[0]['pressure'];

usort($table, 'compare_temp');
$mytemp = $table[0]['temp'];
我现在正尝试将上述函数组合起来,这样我就不必为每次比较都编写一个新的函数,但当与usort一起使用时,我似乎没有任何运气

我需要这样的东西,但不起作用:(


有很多种方式,按优先顺序排列:

// Method 1 -- by object
class Comparitor {
    public function __construct($key) {
        $this->key = $key;
    }
    public function compare($a, $b) {
        return strnatcmp($a[$this->key], $b[$this->key]);
    }
}
usort($table, [new Comparitor('temp'), 'compare']);

// Method 2 -- anonymous function
function compare_by_key($a, $b, $key) {
    return strnatcmp($a[$key], $b[$key]);
}
usort($table, function ($a, $b) { return compare_by_key($a, $b, 'temp'); });

// Method 3 -- by global
function compare_by_global_key($a, $b) {
    global $key;
    return strnatcmp($a[$key], $b[$key]);
}
$key = 'temp';
usort($table, 'compare_by_global_key');    
如果您只需要最大值,则可以使用:

function aggregate_by_key($table, $key, $aggregate = 'max') {
    return $aggregate(array_column($table, $key));
}
$maxTemp = aggregate_by_key($table, 'temp', 'max');
$minTemp = aggregate_by_key($table, 'temp', 'min');
$sumTemp = aggregate_by_key($table, 'temp', 'array_sum');
话虽如此,我还是想从以下几个方面入手:

$max = array_reduce(
    $table,
    function (array $max, array $value) {
        if (strnatcmp($max['temp'], $value['temp']) < 0) {
            $max['temp'] = $value['temp'];
        }
        if (strnatcmp($max['pressure'], $value['pressure']) < 0) {
            $max['pressure'] = $value['pressure'];
        }
        return $max;
    },
    ['temp' => null, 'pressure' => null]
);

echo $max['temp'];
echo $max['pressure'];

因为您要查找的是最高温度/压力值,而不是整个记录,所以请先提取这些值,然后进行排序:

$temps = array_column($weather, 'temp');
natsort($temps);
$max_temp = array_pop($temps);
参考号:

否则,无需对数组进行排序以查找最大值:

function max_by($ary, $fn) {
    $max = reset($ary);
    foreach($ary as $item)
        $max = $fn($max, $item) > 0 ? $max : $item;
    return $max;
}
然后:

$record_with_max_temp = max_by($weather, function($a, $b) {
    return strnatcmp($a['temp'], $b['temp']);
});

这将返回整个记录,而不仅仅是值。

我很想创建一个类来封装整个过程:

class WeatherData{

    private $max_values;

    public function __construct($weather_array, $elements){

        foreach ($weather_array as $weather_item) {
            foreach($elements as $element){
                if(!isset($this->max_values[$element])){
                    $this->max_values[$element] = $weather_item[$element];
                }else{
                    if($this->max_values[$element] < $weather_item[$element]){
                        $this->max_values[$element] = $weather_item[$element];
                    }
                }
            }
        }    
    }

    public function __get($key){
        return $this->max_values[$key];
    }

}

$wa = array(
    array('pressure'=>11, 'temp'=>2),
    array('pressure'=>1, 'temp'=>22),
    array('pressure'=>7, 'temp'=>21),
    array('pressure'=>22, 'temp'=>33),
    array('pressure'=>16, 'temp'=>8),
);
$elements = array(
    'temp', 'pressure'
);
$wd = new WeatherData($wa, $elements);

echo 'max temp: '. $wd->temp . ' max pressure :' . $wd->pressure;
class天气数据{
私人$max_值;
公共函数构造($weather\u数组,$elements){
foreach($weather\u数组作为$weather\u项){
foreach($elements作为$element){
如果(!isset($this->max_values[$element])){
$this->max_values[$element]=$weather_项目[$element];
}否则{
如果($this->max_values[$element]<$weather_item[$element]){
$this->max_values[$element]=$weather_项目[$element];
}
}
}
}    
}
公共功能获取($key){
返回$this->max_值[$key];
}
}
$wa=数组(
阵列('压力'=>11,'温度'=>2),
阵列('压力'=>1,'温度'=>22),
阵列('压力'=>7,'温度'=>21),
阵列('压力'=>22,'温度'=>33),
阵列('压力'=>16,'温度'=>8),
);
$elements=数组(
“温度”,“压力”
);
$wd=新气象数据($wa,$elements);
回显“最大温度:”。$wd->温度。“最大压力:”。$wd->压力;

活生生的例子:

为什么要排序两次?您只需foreach()数组,如果($cur>$prev),则执行
测试压力值和温度值。在数组上循环一次,而不是2个完整的排序。这是最好的方法吗?数组有大约15个值,我需要用大约300个条目进行排序。我想这会使排序更容易,而不必对15个值进行排序。谢谢-看起来简单多了!有一件事我忘了提到我不仅需要最大值,还需要与该值关联的相应时间。@Greg:Ahh,这在方法上有很大的不同!:)
function max_by($ary, $fn) {
    $max = reset($ary);
    foreach($ary as $item)
        $max = $fn($max, $item) > 0 ? $max : $item;
    return $max;
}
$record_with_max_temp = max_by($weather, function($a, $b) {
    return strnatcmp($a['temp'], $b['temp']);
});
class WeatherData{

    private $max_values;

    public function __construct($weather_array, $elements){

        foreach ($weather_array as $weather_item) {
            foreach($elements as $element){
                if(!isset($this->max_values[$element])){
                    $this->max_values[$element] = $weather_item[$element];
                }else{
                    if($this->max_values[$element] < $weather_item[$element]){
                        $this->max_values[$element] = $weather_item[$element];
                    }
                }
            }
        }    
    }

    public function __get($key){
        return $this->max_values[$key];
    }

}

$wa = array(
    array('pressure'=>11, 'temp'=>2),
    array('pressure'=>1, 'temp'=>22),
    array('pressure'=>7, 'temp'=>21),
    array('pressure'=>22, 'temp'=>33),
    array('pressure'=>16, 'temp'=>8),
);
$elements = array(
    'temp', 'pressure'
);
$wd = new WeatherData($wa, $elements);

echo 'max temp: '. $wd->temp . ' max pressure :' . $wd->pressure;