How do you find the minimum and maximum value in php?

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 !

asked May 1, 2011 at 3:06

2

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.

ashleedawg

19.3k7 gold badges69 silver badges100 bronze badges

answered May 1, 2011 at 3:11

RudieRudie

50.3k41 gold badges129 silver badges172 bronze badges

0

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

answered May 1, 2011 at 3:09

Crayon ViolentCrayon Violent

31.6k5 gold badges53 silver badges78 bronze badges

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

answered Jan 24, 2014 at 11:04

RJD22RJD22

10.1k3 gold badges26 silver badges35 bronze badges

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  $max ] {
        $max =  $weight ;
    }

}

echo " min = $min and max = $max \n " ;

answered May 1, 2011 at 6:08

rjha94rjha94

4,1923 gold badges28 silver badges36 bronze badges

1

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

answered Jan 10, 2013 at 6:44

 

answered Nov 11, 2013 at 19:33

HanifeogluHanifeoglu

492 silver badges11 bronze badges

$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 "
".$a."=".$y; if[$y>=$maxvalue]{ $maxvalue = $y; if[$a>$maxsize]{ $maxsize = $a; } } } echo "
max = ".$maxsize;

answered Jan 21, 2016 at 2:29

PanditPandit

7481 gold badge6 silver badges22 bronze badges

1

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

  

answered Jan 17, 2017 at 6:04

1

How do you find the maximum and minimum value of an array in PHP?

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

How do you find the maximum value of a function in PHP?

The max[] function of PHP is used to find the numerically maximum value in an array or the numerically maximum value of several specified values. The max[] function can take an array or several numbers as an argument and return the numerically maximum value among the passed parameters.

What is array_keys [] used for in PHP?

The array_keys[] function returns an array containing the keys.

How do I find the greatest value in an array in PHP?

The max[] function returns the highest value in an array, or the highest value of several specified values.

Chủ Đề