Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

php - min and max in multidimensional-array

hi i am trying to find min and max values of x and y how can i find this min and max functions is not working correctly

$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 ));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I thinik you will have to write your own function:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...