How to get the maximum value from an array in PHP?

To retrieve the maximum value of an array, you can use the built-in PHP function max(). This function will return the maximum value in the array.

Here is an example code:

$array = [2, 5, 9, 1, 4];
$maxValue = max($array);
echo $maxValue; // 输出 9

In this example, we are defining an array $array that contains multiple numbers. Next, we are using the max() function to retrieve the maximum value from this array and storing the result in the variable $maxValue. Finally, we are using the echo function to print out the maximum value.

Note: The max() function can also be used with arrays of strings, comparing them based on alphabetical order.

bannerAds