在PHP数组中获取最小值和最大值。

时间:2021-08-09 03:27:18

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 !

有什么帮助吗?谢谢你!

9 个解决方案

#1


75  

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.) The same as option 1, but to pluck the values, use array_map:

第二个选项。(除非你没有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。如果您只需要一个min或max,那么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。

#2


15  

foreach ($array as $k => $v) {
  $tArray[$k] = $v['Weight'];
}
$min_value = min($tArray);
$max_value = max($tArray);

#3


8  

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

#4


3  

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 " ;

#5


0  

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

#6


0  

<?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/>";
?> 

#7


0  

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

#8


0  

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

?>

? >

#9


0  

print fast five maximum and minimum number from array without use of sorting array in php :-

在php中不使用排序数组,从数组中快速打印5个最大和最小的数字:-。

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

#1


75  

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.) The same as option 1, but to pluck the values, use array_map:

第二个选项。(除非你没有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。如果您只需要一个min或max,那么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。

#2


15  

foreach ($array as $k => $v) {
  $tArray[$k] = $v['Weight'];
}
$min_value = min($tArray);
$max_value = max($tArray);

#3


8  

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

#4


3  

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 " ;

#5


0  

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

#6


0  

<?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/>";
?> 

#7


0  

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

#8


0  

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

?>

? >

#9


0  

print fast five maximum and minimum number from array without use of sorting array in php :-

在php中不使用排序数组,从数组中快速打印5个最大和最小的数字:-。

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