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
你好,我在找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 ));
7 个解决方案
#1
11
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
?>
#2
5
Try something like this:
试试这样:
foreach($dataPoints as $point) {
if (!isset($x) || $point['x'] > $x) {
$x = $point['x'];
}
if (!isset($y) || $point['y'] > $y) {
$y = $point['y'];
}
}
#3
4
I'm a Fan of the Underscore Library
我是下划线库的粉丝
http://brianhaveri.github.com/Underscore.php/#max
http://brianhaveri.github.com/Underscore.php/马克斯
Not native but will save you from having write the function your self, plus many other functions :)
不是本地的,但是会让你不用写自己的功能,还有很多其他的功能:)
oh and it becomes a one-liner!
哦,它变成了一句俏皮话!
Example:
例子:
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
4
You can use PHP's array_column() (PHP 5 >= 5.5.0) for this:
您可以使用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' ) );
#5
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];
#6
1
You can find individually the minimum and maximum values of each column by combining array_column, min and max functions:
结合array_column、min和max函数,分别求出各列的最小值和最大值:
$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'));
#7
0
Sample code to built your required answer
构建所需答案的示例代码
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
}
#1
11
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
?>
#2
5
Try something like this:
试试这样:
foreach($dataPoints as $point) {
if (!isset($x) || $point['x'] > $x) {
$x = $point['x'];
}
if (!isset($y) || $point['y'] > $y) {
$y = $point['y'];
}
}
#3
4
I'm a Fan of the Underscore Library
我是下划线库的粉丝
http://brianhaveri.github.com/Underscore.php/#max
http://brianhaveri.github.com/Underscore.php/马克斯
Not native but will save you from having write the function your self, plus many other functions :)
不是本地的,但是会让你不用写自己的功能,还有很多其他的功能:)
oh and it becomes a one-liner!
哦,它变成了一句俏皮话!
Example:
例子:
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
4
You can use PHP's array_column() (PHP 5 >= 5.5.0) for this:
您可以使用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' ) );
#5
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];
#6
1
You can find individually the minimum and maximum values of each column by combining array_column, min and max functions:
结合array_column、min和max函数,分别求出各列的最小值和最大值:
$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'));
#7
0
Sample code to built your required answer
构建所需答案的示例代码
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
}