How to find the maximum and minimum values of php array
This article mainly introduces the relevant knowledge of "how to find the maximum and minimum values of the php array". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "how to find the maximum and minimum values of the php array" can help you solve the problem.
There are two methods: 1. Use the "max" statement to get the maximum value and the "min" statement to get the minimum value. 2. Use "sort" to sort the array in ascending order, "array_key_first" to get the minimum value, and "array_key_last (array)" to get the maximum value.
Operating environment of this tutorial: windows7 system, PHP7.5 version, DELL G3 computer
In php, there are many ways to get the maximum and minimum values of an array. Here are two ways:
Method 1: use the max () and min () functions
The max () function returns the maximum value in an array
The min () function returns the minimum value in an array
Example:
Method 2: use the sort (), array_key_first (), array_key_last () functions
You can use sort () to sort the array in ascending order
At this point, you can see that the first element of the array is the minimum value, and the last element is the maximum value. You only need to take out these two values.
So how to get the first and last element of the array, you can use the array_key_first () and array_key_last () functions
Array_key_first () gets the first key value of the specified array.
Array_key_last () gets the last key value of the specified array.
The maximum value of the echo array is: ".array _ key_last ($arr)."
The maximum value of the "; echo" array is: ".array _ key_first ($arr)."
"
Note: PHP versions of these two functions: PHP 7 > = 7.3.0, PHP 8
This is the end of the content about "how to find the maximum and minimum values of the php array". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.