2010-12-21 232 views
5

你好我試圖找到分鐘,我怎麼能找到這個MIN和MAX函數x和y的最大值無法正常工作最小和最大

$dataPoints = array(
array('x' => 2343, 'y' => 4322), 
    array('x' => 103, 'y' => 303), 
    array('x' => 2345,'y' => 2321), 
    array('x' => 310, 'y' => 2044), 
    array('x' => 173, 'y' => 793), 
    array('x' => 456, 'y' => 2675), 
    array('x' => 24, 'y' => 819)); 

回答

11

我thinik你將不得不寫自己的功能:

<?php 
    function max_with_key($array, $key) { 
     if (!is_array($array) || count($array) == 0) return false; 
     $max = $array[0][$key]; 
     foreach($array as $a) { 
      if($a[$key] > $max) { 
       $max = $a[$key]; 
      } 
     } 
     return $max; 
    } 


    $dataPoints = array(
    array('x' => 2343, 'y' => 4322), 
     array('x' => 103, 'y' => 303), 
     array('x' => 2345,'y' => 2321), 
     array('x' => 310, 'y' => 2044), 
     array('x' => 173, 'y' => 793), 
     array('x' => 456, 'y' => 2675), 
     array('x' => 24, 'y' => 819)); 

    $max_x = max_with_key($dataPoints, 'x'); //2343 
    $max_y = max_with_key($dataPoints, 'y'); //4322 
?> 
+0

I有沒有任何方法來獲得最大值的數組不僅是最大值 – Bugfixer 2015-02-21 13:25:34

+0

Magnificient。但是如果你也需要數組鍵的話,你需要改變foreach以包含鍵(數組......),因爲邏輯警察會逮捕你,否則改變if如下所示:'$ max [$ logicpolice ] = $ a [$ key];' – kry 2018-02-21 10:25:24

5

嘗試這樣:

foreach($dataPoints as $point) { 
    if (!isset($x) || $point['x'] > $x) { 
     $x = $point['x']; 
    } 
    if (!isset($y) || $point['y'] > $y) { 
     $y = $point['y']; 
    } 
} 
+0

記得加$ x = array(); $ y = array();在foreach之前。 – 2015-05-31 12:39:26

0

樣品合作德內置您所需答案

foreach($dataPoints[0]['x'] as $x_val) 
{ 
//write max min func for x values 
} 

foreach($dataPoints[0]['y'] as $y_val) 
{ 
//write max min func for y values 
} 
2
//to find max use rsort() & for min :sort() 
//Below will return you the max 
$dataPoints = array(
    array('x' => 2343, 'y' => 4322), 
     array('x' => 103, 'y' => 7303), 
     array('x' => 2345,'y' => 2321), 
     array('x' => 310, 'y' => 2044), 
     array('x' => 173, 'y' => 793), 
     array('x' => 456, 'y' => 2675), 
     array('x' => 24, 'y' => 819)); 

     foreach ($dataPoints as $key=>$value) { 
     $x[$key] = $value['x']; 
     $y[$key] = $value['y']; 
     } 
rsort($x,SORT_DESC); 
rsort($y,SORT_DESC); 

echo $x[0]; 
echo $y[0]; 
4

我的下劃線圖書館

http://brianhaveri.github.com/Underscore.php/#max

不是本地但粉絲將節省你不必寫函數你的自我 ,加上許多其他功能:)

哦,它成爲一個單線!

實施例:

include_once 'underscore.php'; 

$dataPoints = array(
    array('x' => 2343, 'y' => 4322), 
    array('x' => 103, 'y' => 303), 
    array('x' => 2345,'y' => 2321), 
    array('x' => 310, 'y' => 2044), 
    array('x' => 173, 'y' => 793), 
    array('x' => 456, 'y' => 2675), 
    array('x' => 24, 'y' => 819) 
); 


__::max($dataPoints, function($item) { return $item['x']; }); // array('x' => 2345,'y' => 2321) 
__::min($dataPoints, function($item) { return $item['y']; }); // array('x' => 103, 'y' => 303) 
4

可以使用PHP的array_column()(PHP 5> = 5.5.0),用於這樣的:

$min_x = min(array_column($dataPoints, 'x')); 
$min_y = min(array_column($dataPoints, 'y')); 
$max_x = max(array_column($dataPoints, 'x')); 
$max_x = max(array_column($dataPoints, 'y')); 
1

可以找到單獨的最小和通過每一列的最大值結合array_column,minmax功能:

$min_x = min(array_column($dataPoints, 'x')); 
$max_x = max(array_column($dataPoints, 'x')); 
$min_y = min(array_column($dataPoints, 'y')); 
$max_y = max(array_column($dataPoints, 'y'));