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 $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
PHP get max value in array using for 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:
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:
Conclusion
Through this tutorial, we have learned how to find or get max value or elements from array in PHP.
Recommended PHP Tutorials
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