Find largest number in array php using loop

This should work for you:


Output:

68

In your example you just did 2 things wrong:

$a = array(1, 44, 5, 6, 68, 9);
$res = $a[0];

for($i = 0; $i <= count($a); $i++) {
              //^ equal is too much gives you an offset!

      if($res > $a[$i]){
            //^ Wrong condition change it to < 
          $res=$a[$i];
      }

}

EDIT:

With a for loop:

$a = array(1, 44, 5, 6, 68, 9);
$res = 0;

for($count = 0; $count < count($a); $count++) {

    if($res < $a[$count])
        $res = $a[$count];

}

How to get maximum value from array in PHP using Library function, foeach Loop, for loop

PHP get max value in array using Library function

PHP get max value in array using foreach

$val)
 {
    if ($val > $b) {
        $b = $val;
    }
}
echo $b;
?>

PHP get max value in array using for loop

 $b)
	{
        $b = $arr[$i];
    }
}
echo $b;
?>

Find largest number in array php using loop

PHP get the max/maximum/largest value from array. This tutorial has the purpose to explain to you several easy ways to find or get the maximum or largest value from an array in PHP.

Here, we will take e.g. PHP gets the maximum value in an array, get the largest number in the array PHP without a function or get the max value from an array in PHP using for loop

PHP find the highest value in an array

We can use the PHP max() function, which is used to find the largest number from an array in PHP.

Before we use the take examples, you must know about the PHP max() function.

PHP max() function

The max() function is inbuilt PHP function, which is used to find the numerically maximum or highest value in an array or find maximum or highest value of given specified values.

Syntax

 max(array)  
 or
 max(value1, value2, value3 … valueN)

Example – 1 Get max value in array PHP using max() function

Let’s take the first example, we will use the PHP max() function to find the maximum value in the array. Let’s see the example below:

 

The output of the above program is: 100

Example – Find largest number in array php without function

Let’s take the second example, in this example, we will find or get the largest or maximum number in array PHP without function. Let’s see the example below:

 $val){

    if($max < $val){

        $max = $val;
        
    }
}	

print $max;

?> 

The output of the above program is: 1000

Example – 3 PHP get max or highest value in array using for loop

Let’s take the third example, to find the maximum or largest or highest value in array PHP without using any function. Let’s look at the examples below:

 $max)
	{
        $max = $arr[$i];
    }
}

print $max;

?>

Conclusion

Through this tutorial, we have learned how to find or get max value or elements from array in PHP.

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

View all posts by Admin

Post navigation

How to find max value from array 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.

How to get max value in foreach PHP?

PHP get max value in array using foreach php $arr= array(110, 20, 52 ,105, 56, 89, 96); //using foreach loop $b = 0; foreach ($arr as $key=>$val) { if ($val > $b) { $b = $val; } } echo $b; ?>

How do I find the smallest and largest number in 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);