获取 PHP 数组中的最小值和最大值

发布于 2024-11-04 16:50:44 字数 628 浏览 0 评论 0原文

我有一个像这样的数组:


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

我需要提取最小和最大权重值。 在此示例中

$min_value = 175

$max_value = 200

关于如何执行此操作有任何帮助吗? 谢谢 !

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

$max_value = 200

Any help on how to do this ?
Thank you !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

伤痕我心 2024-11-11 16:50:44

选项 1。 首先映射数组以获取这些数字(而不是完整的详细信息):

$numbers = array_column($array, 'weight')

然后获得最小值和最大值:

$min = min($numbers);
$max = max($numbers);

选项 2。(仅当您不这样做时)没有 PHP 5.5 或更高版本)与选项 1 相同,但要提取值,请使用 array_map

$numbers = array_map(function($details) {
  return $details['Weight'];
}, $array);

选项 3。

选项 4。如果您只需要最小值或最大值,array_reduce() 可能会更快:

$min = array_reduce($array, function($min, $details) {
  return min($min, $details['weight']);
}, PHP_INT_MAX);

这会执行更多 min(),但速度非常快。 PHP_INT_MAX 是从高位开始,然后越来越低。您可以对 $max 执行相同的操作,但要从 0-PHP_INT_MAX 开始。

Option 1. First you map the array to get those numbers (and not the full details):

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

$numbers = array_map(function($details) {
  return $details['Weight'];
}, $array);

Option 3.

Option 4. If you only need a min OR max, array_reduce() might be faster:

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

回忆躺在深渊里 2024-11-11 16:50:44
foreach ($array as $k => $v) {
  $tArray[$k] = $v['Weight'];
}
$min_value = min($tArray);
$max_value = max($tArray);
foreach ($array as $k => $v) {
  $tArray[$k] = $v['Weight'];
}
$min_value = min($tArray);
$max_value = max($tArray);
雅心素梦 2024-11-11 16:50:44

对于使用 PHP 5.5+ 的人来说,使用 array_column 可以更轻松地完成此操作。不再需要那些丑陋的 array_maps 了。

如何获取最大值:

$highest_weight = max(array_column($details, 'Weight'));

如何获取最小值

$lowest_weight = min(array_column($details, 'Weight'));

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.

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'));
扬花落满肩 2024-11-11 16:50:44

有趣的是,上面的两个解决方案都使用数组形式的额外存储(第一个解决方案使用两个数组,第二个解决方案使用一个数组),然后您使用“额外存储”数组找到最小值和最大值。虽然这在现实编程世界中可能是可以接受的(谁给出了关于“额外”存储的两位数?),但它会让您在编程 101 中获得“C”。

只需额外两个即可轻松解决查找最小值和最大值的问题内存插槽

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

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.

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 " ;
倾其所爱 2024-11-11 16:50:44

不使用 minmax 等预定义函数怎么样?

    //find max 
     $arr = [4,5,6,1];
        $val = $arr[0];
        $n = count($arr);
        for($i=0;$i<$n;$i++) {
        if($val < $arr[$i]) {
            $val = $arr[$i];
        }
        }
        echo $val;


     //find min 
     $arr = [4,5,6,1];
        $val = $arr[0];
        $n = count($arr);
        for($i=0;$i<$n;$i++) {
        if($val > $arr[$i]) {
            $val = $arr[$i];
        }
        }
        echo $val;

How about without using predefined functions like min or max ?

    //find max 
     $arr = [4,5,6,1];
        $val = $arr[0];
        $n = count($arr);
        for($i=0;$i<$n;$i++) {
        if($val < $arr[$i]) {
            $val = $arr[$i];
        }
        }
        echo $val;


     //find min 
     $arr = [4,5,6,1];
        $val = $arr[0];
        $n = count($arr);
        for($i=0;$i<$n;$i++) {
        if($val > $arr[$i]) {
            $val = $arr[$i];
        }
        }
        echo $val;
慕巷 2024-11-11 16:50:44
$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);
$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);
思念满溢 2024-11-11 16:50:44
<?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/>";
?> 
<?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/>";
?> 
尝蛊 2024-11-11 16:50:44
$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;
$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;
墨小沫ゞ 2024-11-11 16:50:44

从数组中快速打印五个最大和最小数字,而不使用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; 
}
?>  

print fast five maximum and minimum number from array without use of sorting array in 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; 
}
?>  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文