多维数组中的最小值和最大值

时间:2010-12-21 09:28:42

标签: php multidimensional-array

嗨我想找到x和y的最小值和最大值我怎么能找到这个min和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 ));

7 个答案:

答案 0 :(得分:11)

我认为你必须编写自己的函数:

<?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
?>

答案 1 :(得分:5)

尝试这样的事情:

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

答案 2 :(得分:5)

您可以使用PHP的array_column()(PHP 5&gt; = 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' ) );

答案 3 :(得分: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 :(得分: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];

答案 5 :(得分:1)

您可以通过合并array_columnminmax函数单独找到每列的最小值和最大值:

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

答案 6 :(得分: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
}