您好,我正在尝试查找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 ));
最佳答案
我认为您将必须编写自己的函数:
<?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
?>
关于php - 多维数组中的最小值和最大值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4497810/