I have an array i want to find max value for both positive and negative value. The expected result would be -13.2 and 7.8
$array = array
(
[0] => -13.2
[1] => -14.5
[2] => -14.3
[3] => -13.7
[4] => -13.8
[5] => -14.6
[6] => 6.4
[7] => 6.9
[8] => 7.2
[9] => 6.9
[10] => 7.8
[11] => 6.9
[12] => 6.3
[13] => 7.2
[14] => 6.9
[15] => 6.8
)
$maxrl='';
for($i=1,$j=0;$i<=count($array);$i++,$j++)
{
if($maxr <= $array[$j])
{
if(($maxr < $array[$j]) && ($maxrl != ''))
{
$maxrl='';
$maxrl.="L".$k.",";
}
else
{
$maxrl.="L".$k.",";
}
$maxr = $max_array[$j];
}
$k++;
}
echo "<tr><td >'.$maxr.'</td><td>'.substr($maxrl,0,-1).'</td><>/tr>";
Barmar
787k57 gold badges553 silver badges665 bronze badges
asked Mar 20, 2014 at 10:52
5 Answers 5
You can simply use max & min functions,
echo "Max Value:- ".max($array); // Max Value:- 7.8
echo "Min Value:- ".min($array); // Min Value:- -14.6
UPDATED: If you really want max from both negative & positive values,
$positive = array_filter($array, function ($v) {
return $v > 0;
});
$negative = array_filter($array, function ($v) {
return $v < 0;
});
echo "Max in positive Value:- ".max($positive); // Max in positive Value:- 7.8
echo "Min in negative Value:- ".min($negative); // Max in negative Value:- -13.2
answered Mar 20, 2014 at 10:54
-
-14.6
actually ;)Shankar Narayana Damodaran– Shankar Narayana Damodaran2014年03月20日 10:56:50 +00:00Commented Mar 20, 2014 at 10:56 -
@ShankarDamodaran - Corrected. sharp eyes :)Rikesh– Rikesh2014年03月20日 10:57:21 +00:00Commented Mar 20, 2014 at 10:57
-
2But the answer he wants for the negatives is -13.2, the maximum negative number.Barmar– Barmar2014年03月20日 10:59:40 +00:00Commented Mar 20, 2014 at 10:59
-
@Barmar - Totally agree with you,was just updating my answer.Rikesh– Rikesh2014年03月20日 11:01:10 +00:00Commented Mar 20, 2014 at 11:01
-
Shankar - Thanks. We all just miss-understood the question on first look. Thanks to @Barmar :)Rikesh– Rikesh2014年03月20日 11:03:32 +00:00Commented Mar 20, 2014 at 11:03
For the positive maximum just take
$max = max($array);
The highest minimum is a bit more complex:
$minArray = array();
foreach ( $array as $val )
if ( $val < 0 )
$minArray[] = $val;
$min = max($minArray);
If I get the OPs question right
answered Mar 20, 2014 at 11:05
PHP has max
and min
methods specifically for this
$max = max($array); // 7.8
$min = min($array); // -14.6
answered Mar 20, 2014 at 10:53
//$dd = array(-50, -25, -5, -80, -40, -152, -45, -28, -455, -100, -98, -455);
$dd = array(50, -25, -5, 80, -40, -152, -45, 28, -455, 100, 98, -455);
//$dd = array(50, 25, 5, 80, 40, 152, 45, 28, 455, 100, 98, 455);
$curr = '';
$max = $dd[0];
for($i = 0; $i < count($dd); $i++) {
$curr = $dd[$i];
if($curr >= $max) {
$max = $curr;
}
}
echo "greatest number is $max";
answered Aug 28, 2016 at 10:01
Just do:
$max = max($array);
$min = min($array);
Much easier! ;)
answered Mar 20, 2014 at 10:56
-
Why the -1? This is the standard way of doing it!Thomas Hansen– Thomas Hansen2014年03月20日 11:17:15 +00:00Commented Mar 20, 2014 at 11:17
lang-php
-14.6
not-13.2
$maxNegative = max(array_filter($myArray, function($value){return $value<0;}));
min($array)
won't work. One needs to split the array in positive and negative values and get the max from the negative ones.