Get min and max value in PHP Array

后端 未结 9 1824
天涯浪人
天涯浪人 2020-11-30 00:36

I have an array like this:


array (0 => 
  array (
    \'id\' => \'20110209172713\',
    \'Date\' => \'2011-02-09\',
    \'Weight\' => \'200\',
  ),
  1 =>          


        
相关标签:
9条回答
  • 2020-11-30 00:58
    foreach ($array as $k => $v) {
      $tArray[$k] = $v['Weight'];
    }
    $min_value = min($tArray);
    $max_value = max($tArray);
    
    0 讨论(0)
  • 2020-11-30 01:03
    <?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/>";
    ?> 
    
    0 讨论(0)
  • 2020-11-30 01:09
    $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;
    
    0 讨论(0)
  • 2020-11-30 01:09

    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; 
    }
    ?>  
    
    0 讨论(0)
  • 2020-11-30 01:11

    How about without using predefined function like min or 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);
    

    ?>

    0 讨论(0)
  • 2020-11-30 01:18

    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'));
    
    0 讨论(0)
提交回复
热议问题