2014-09-24 27 views
1

我有一個包含天氣值的數組。我現在有一個可用的php腳本來對溫度進行排序,然後提取溫度的最大值,然後再用壓力對陣列進行排序並獲得最大壓力值。與usort結合使用時的功能?

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使用時要任何運氣。

我需要這樣的事情,這不工作:(

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

usort($table, 'compare_temp', 'temp'); 
+1

爲什麼要分兩次?你可以foreach()數組,並執行'if($ cur> $ prev)'測試壓力和溫度值。一個循環遍歷數組,而不是2個完整的排序 – 2014-09-24 14:24:38

+0

這是否是最好的方法 - 數組有大約15個值,我需要對大約300個條目進行排序。我想這會讓它更容易,而不必排序15個值。 – Greg 2014-09-24 14:29:51

回答

1

很多方面,按優先順序:

// 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'); 
+0

我忘記提及的一件事是,我不僅需要最大值,而且需要與該值相關的相應時間。 – Greg 2014-09-24 14:50:29

+0

@Greg:啊,這在方法上有很大的不同! :) – bishop 2014-09-24 14:52:46

+0

謝謝 - 我已經全部使用方法1 – Greg 2014-09-25 09:47:20

0
function createComparisonFunction($key) { 
    return function ($a, $b) use ($key) { 
     return strnatcmp($a[$key], $b[$key]); 
    }; 
} 

usort($table, createComparisonFunction('pressure')); 
usort($table, createComparisonFunction('temp')); 

有賽d,是,我寧願用這種方式來開始:

$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']; 

我不知道爲什麼你需要strnatcmp真的,這些值應爲整數,這將簡化回調函數,以這樣的:

function (array $max, array $value) { 
     $max['temp']  = max($max['temp'], $value['temp']); 
     $max['pressure'] = max($max['pressure'], $value['pressure']); 
     return $max; 
    } 
+0

謝謝 - 似乎更簡單! – Greg 2014-09-24 14:37:27

0

我會受到誘惑,創建一個類來encaptulate的全過程:

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; 

活生生的例子:http://codepad.viper-7.com/kobe1Z