1

I want to find the maximum and minimum value by the for loop, but problem is that when array value is start from a maximum number like (100,30,50,60) then output is the correct first maximum value then, minimum value.

Otherwise output first minimum value, then maximum value.

any idea?

    //Number serial.
    $number = array(10,15,20,100,25,30);

    for ($i=0; $i<count($number); $i++){
        //Find maximum number by max function.
        if ($number[$i] == max($number)){
            //Print maximum number.
            echo " The max number is $number[$i] <br>" ;
        }
        //Find minimum number by min function.
        elseif ($number[$i] == min($number)) {
            //Print minimum  number.
            echo " The min number is $number[$i] <br>";
        }
    }
//Output: 
//The min number is 10
//The max number is 100 

When array value is start from a maximum number.

 //Number serial.
    $number = array(100,10,15,20,25,30);

    for ($i=0; $i<count($number); $i++){
        //Find maximum number by max function.
        if ($number[$i] == max($number)){
            //Print maximum number.
            echo " The max number is $number[$i] <br>" ;
        }
        //Find minimum number by min function.
        elseif ($number[$i] == min($number)) {
            //Print minimum  number.
            echo " The min number is $number[$i] <br>";
        }
    }
//Output:
//The max number is 100
//The min number is 10
3
  • why dont you just use sort() instead of a loop. w3schools.com/php/php_arrays_sort.asp Secondly, you might want to save the max and min values in different variables, in the loop and then echo them at the end. Commented Feb 10, 2016 at 3:23
  • 4
    since your already using min() and max(). why not just remove the for loop? Commented Feb 10, 2016 at 3:26
  • 1
    could you paraphrase the question. it seems to do the correct behavior, the second codeblock first checks for the maximum and the first element is the maximum, in turn, echoes it Commented Feb 10, 2016 at 3:26

3 Answers 3

1

Actually I think you dont need the for loop to begin with.... You can do something simple like this:

$number = array(15,20,100,10,25,30);
$max = max($number);
$min = min($number);
echo "min value is $min <br/>";
echo "max value is $max <br/>";

Update:

But if you want to flexibly, echo the statements according to the index of the min and max values then you can do something like this:

$number = array(15,20,100,10,25,30);
$max = max($number);
$min = min($number);
$max_idx = array_search($max,$number);
$min_idx = array_search($min,$number);
if($max_idx < $min_idx){
  echo "max value is $max \n";
  echo "min value is $min \n";
}else{
  echo "min value is $min \n";
  echo "max value is $max \n";
}

What this will do is, it will get the indices of the min and max values and depending on which is encountered first, it will print that statement first.

Hope it helps

Sign up to request clarification or add additional context in comments.

1 Comment

@RajDev Updated answer, now you can choose which statement to print first
0

Use Below Code.

$number = array(15,20,100,10,25,30);

for ($i=0; $i<count($number); $i++){
    //Find maximum number by max function.
    if ($number[$i] == max($number)){
        //Print maximum number.
        $max = $number[$i];
    }
    //Find minimum number by min function.
    elseif ($number[$i] == min($number)) {
    //Print minimum  number.
        $min = $number[$i];
    }
}

echo "min value is $min <br/>";
echo "max value is $max <br/>";

Comments

0
$a = [3,4,5];
$b = $a[0];
$c = $a[0];

for ($i=0; $i < sizeof($a) ; $i++) { 
    if($a[$i] < $b){
        $b = $a[$i];
    }else if($a[$i] > $c){
        $c = $a[$i];
    }
}

echo $b;
echo "<br>";
echo $c;

Comments

Your Answer

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.