获取 PHP 数组中的最小值和最大值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5846156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get min and max value in PHP Array
提问by Peter
I have an array like this:
我有一个这样的数组:
array (0 =>
array (
'id' => '20110209172713',
'Date' => '2011-02-09',
'Weight' => '200',
),
1 =>
array (
'id' => '20110209172747',
'Date' => '2011-02-09',
'Weight' => '180',
),
2 =>
array (
'id' => '20110209172827',
'Date' => '2011-02-09',
'Weight' => '175',
),
3 =>
array (
'id' => '20110211204433',
'Date' => '2011-02-11',
'Weight' => '195',
),
)
I need to extract minimal and maximal Weight values. In this example
我需要提取最小和最大权重值。在这个例子中
$min_value = 175
$min_value = 175
$max_value = 200
$max_value = 200
Any help on how to do this ? Thank you !
有关如何执行此操作的任何帮助?谢谢 !
回答by Rudie
Option 1.First you map the array to get those numbers (and not the full details):
选项 1.首先映射数组以获取这些数字(而不是完整的详细信息):
$numbers = array_column($array, 'weight')
Then you get the min and max:
然后你得到最小值和最大值:
$min = min($numbers);
$max = max($numbers);
Option 2.(Only if you don't have PHP 5.5 or better) The same as option 1, but to pluck the values, use array_map
:
选项 2.(仅当您没有 PHP 5.5 或更高版本时)与选项 1 相同,但要提取值,请使用array_map
:
$numbers = array_map(function($details) {
return $details['Weight'];
}, $array);
Option 3.
选项 3。
Option 4.If you only need a min OR max, array_reduce()
might be faster:
选项 4.如果您只需要最小或最大,array_reduce()
可能会更快:
$min = array_reduce($array, function($min, $details) {
return min($min, $details['weight']);
}, PHP_INT_MAX);
This does more min()
s, but they're very fast. The PHP_INT_MAX
is to start with a high, and get lower and lower. You could do the same for $max
, but you'd start at 0
, or -PHP_INT_MAX
.
这会做更多min()
的事情,但他们非常快。该PHP_INT_MAX
是具有高启动,并得到越来越低。您可以对 执行相同的操作$max
,但您将从0
, 或开始-PHP_INT_MAX
。
回答by Crayon Violent
foreach ($array as $k => $v) {
$tArray[$k] = $v['Weight'];
}
$min_value = min($tArray);
$max_value = max($tArray);
回答by RJD22
For the people using PHP 5.5+ this can be done a lot easier with array_column. Not need for those ugly array_maps anymore.
对于使用 PHP 5.5+ 的人来说,使用array_column可以更容易地做到这一点。不再需要那些丑陋的 array_maps。
How to get a max value:
如何获得最大值:
$highest_weight = max(array_column($details, 'Weight'));
How to get the min value
如何获得最小值
$lowest_weight = min(array_column($details, 'Weight'));
回答by rjha94
It is interesting to note that both the solutions above use extra storage in form of arrays (first one two of them and second one uses one array) and then you find min and max using "extra storage" array. While that may be acceptable in real programming world (who gives a two bit about "extra" storage?) it would have got you a "C" in programming 101.
有趣的是,上述两种解决方案都使用数组形式的额外存储(第一个两个,第二个使用一个数组),然后您使用“额外存储”数组找到最小值和最大值。虽然这在真实的编程世界中可能是可以接受的(谁给出了两个关于“额外”存储的信息?)它会让你在编程 101 中获得“C”。
The problem of finding min and max can easily be solved with just two extra memory slots
只需两个额外的内存插槽就可以轻松解决寻找最小值和最大值的问题
$first = intval($input[0]['Weight']);
$min = $first ;
$max = $first ;
foreach($input as $data) {
$weight = intval($data['Weight']);
if($weight <= $min ) {
$min = $weight ;
}
if($weight > $max ) {
$max = $weight ;
}
}
echo " min = $min and max = $max \n " ;
回答by Bhanwar
$num = array (0 => array ('id' => '20110209172713', 'Date' => '2011-02-09', 'Weight' => '200'),
1 => array ('id' => '20110209172747', 'Date' => '2011-02-09', 'Weight' => '180'),
2 => array ('id' => '20110209172827', 'Date' => '2011-02-09', 'Weight' => '175'),
3 => array ('id' => '20110211204433', 'Date' => '2011-02-11', 'Weight' => '195'));
foreach($num as $key => $val)
{
$weight[] = $val['Weight'];
}
echo max($weight);
echo min($weight);
回答by Hanifeoglu
<?php
$array = array (0 =>
array (
'id' => '20110209172713',
'Date' => '2011-02-09',
'Weight' => '200',
),
1 =>
array (
'id' => '20110209172747',
'Date' => '2011-02-09',
'Weight' => '180',
),
2 =>
array (
'id' => '20110209172827',
'Date' => '2011-02-09',
'Weight' => '175',
),
3 =>
array (
'id' => '20110211204433',
'Date' => '2011-02-11',
'Weight' => '195',
),
);
foreach ($array as $key => $value) {
$result[$key] = $value['Weight'];
}
$min = min($result);
$max = max($result);
echo " The array in Minnumum number :".$min."<br/>";
echo " The array in Maximum number :".$max."<br/>";
?>
回答by Pandit
$Location_Category_array = array(5,50,7,6,1,7,7,30,50,50,50,40,50,9,9,11,2,2,2,2,2,11,21,21,1,12,1,5);
asort($Location_Category_array);
$count=array_count_values($Location_Category_array);//Counts the values in the array, returns associatve array
print_r($count);
$maxsize = 0;
$maxvalue = 0;
foreach($count as $a=>$y){
echo "<br/>".$a."=".$y;
if($y>=$maxvalue){
$maxvalue = $y;
if($a>$maxsize){
$maxsize = $a;
}
}
}
echo "<br/>max = ".$maxsize;
回答by Sambhav Pandey
How about without using predefined function like min
or max
?
不使用预定义的函数如min
或max
怎么样?
$arr = [4,5,6,7,8,2,9,1];
$val = $arr[0];
$n = count($arr);
for($i=1;$i<$n;$i++) {
if($val<$arr[$i]) {
$val = $val;
} else {
$val = $arr[$i];
}
}
print($val);
?>
?>
回答by dhavaloza
print fast five maximum and minimum number from array without use of sorting array in php :-
在 php 中不使用排序数组的情况下从数组中快速打印五个最大和最小数:-
<?php
$array = explode(',',"78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 81, 76, 73,
68, 72, 73, 75, 65, 74, 63, 67, 65, 64, 68, 73, 75, 79, 73");
$t=0;
$l=count($array);
foreach($array as $v)
{
$t += $v;
}
$avg= $t/$l;
echo "average Temperature is : ".$avg." ";
echo "<br>List of seven highest temperatsures :-";
$m[0]= max($array);
for($i=1; $i <7 ; $i++)
{
$m[$i]=max(array_diff($array,$m));
}
foreach ($m as $key => $value) {
echo " ".$value;
}
echo "<br> List of seven lowest temperatures : ";
$mi[0]= min($array);
for($i=1; $i <7 ; $i++)
{
$mi[$i]=min(array_diff($array,$mi));
}
foreach ($mi as $key => $value) {
echo " ".$value;
}
?>