0

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
8
  • "The maximum for a negative value" usual is called "minimum" ... Commented Mar 20, 2014 at 10:54
  • 2
    It should return -14.6 not -13.2 Commented Mar 20, 2014 at 10:55
  • Do you mean the highest negative value, or the lowest? $maxNegative = max(array_filter($myArray, function($value){return $value<0;})); Commented Mar 20, 2014 at 10:56
  • 2
    @ShankarDamodaran \ I think he wants the highest negative value, that's why the expected answer is -13.2. Commented Mar 20, 2014 at 10:58
  • If the OP wants -13.2 as a result, that would mean the maximum of the negative values. And then just min($array) won't work. One needs to split the array in positive and negative values and get the max from the negative ones. Commented Mar 20, 2014 at 10:59

5 Answers 5

3

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
6
  • -14.6 actually ;) Commented Mar 20, 2014 at 10:56
  • @ShankarDamodaran - Corrected. sharp eyes :) Commented Mar 20, 2014 at 10:57
  • 2
    But the answer he wants for the negatives is -13.2, the maximum negative number. Commented Mar 20, 2014 at 10:59
  • @Barmar - Totally agree with you,was just updating my answer. Commented Mar 20, 2014 at 11:01
  • Shankar - Thanks. We all just miss-understood the question on first look. Thanks to @Barmar :) Commented Mar 20, 2014 at 11:03
2

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
1

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
0
//$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
-3

Just do:

$max = max($array);
$min = min($array);

Much easier! ;)

answered Mar 20, 2014 at 10:56
1
  • Why the -1? This is the standard way of doing it! Commented Mar 20, 2014 at 11:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.