Get Min and Max Value in PHP Array

Get min and max value in PHP Array

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.

find the minimum and maximum value in array group php

Try this:

foreach ($your_array as $subarr) {
echo $subarr[package]." minimum = ";
echo min($subarr[value])." and maximum = ";
echo max($subarr[value])."<br>";
}

this will output each package name together with the minimum and maximum values.

Get min/max value from array based on condition

array_filter to the rescue!

Before checking for min, remove the elements from your array which have Quantity set to 0.

echo min( array_column( array_filter($array,function($v) { 
return $v["Quantity"] > 0; }), 'Price' ) );

To make it more readable

$filtered=array_filter($array,function($v) { return $v["Quantity"] > 0; });
echo min( array_column( $filtered, 'Price' ) );

Fiddle

And an old-school version without all the closures

foreach($array as $v)
{
if($v["Quantity"]>0 && (!$min || $v["Price"]<$min))
$min=$v["Price"];
}

Fiddle

PHP - get min and max values of numbers with special difference to each other

It's alway better to show some of the code you have tried so far.
However here is a very short code example I put together.

$lists       = [];
$special = 1800;
$array = [0, 1800, 3600, 5400, 7200, 12600, 14400, 16200, 23400, 25200];
$currentList = [];
foreach ($array as $number) {
if (empty($currentList)) {
$currentList[] = $number;
} else {
$last =(end($currentList) + $special);
if ($number === $last) {
$currentList[] = $number;
} else {
$lists[] = $currentList;
$currentList = [$number];
}
}
}
$lists[] = $currentList;
var_dump($lists);

This will output the following array, which could be transformed in the output you want.

array (size=3)   0 => 
array (size=5)
0 => int 0
1 => int 1800
2 => int 3600
3 => int 5400
4 => int 7200 1 =>
array (size=3)
0 => int 12600
1 => int 14400
2 => int 16200 2 =>
array (size=2)
0 => int 23400
1 => int 25200

Find min/max in a two dimensional array

Here's one way to get the min and max values:

$min = min(array_column($array, 'Price'));
$max = max(array_column($array, 'Price'));

To return the nested array for the min and max:

$prices = array_column($array, 'Price');
$min_array = $array[array_search(min($prices), $prices)];
$max_array = $array[array_search(max($prices), $prices)];

You could do each in one line since that looked like what you were trying to do:

$min_array = $array[array_search(min($prices = array_column($array, 'Price')), $prices)];
$max_array = $array[array_search(max($prices = array_column($array, 'Price')), $prices)];

PHP >= 5.5.0 needed for array_column() or use the PHP Implementation of array_column().

Using array_map() to get just the min and max:

$min = min(array_map(function($a) { return $a['Price']; }, $array));
$max = max(array_map(function($a) { return $a['Price']; }, $array));

There's probably a good array_filter() or array_reduce() as well.



Related Topics



Leave a reply



Submit