Home >Backend Development >PHP Problem >How to find the difference between the maximum value and the minimum value of an array in php
Steps to find the difference: 1. Use the max() function to get the maximum value of the array, the syntax "max($arr)"; 2. Use the min() function to get the minimum value of the array, the syntax "min($ arr)"; 3. Use the "-" operator to subtract the obtained maximum value and minimum value to calculate the difference. The syntax is "maximum value - minimum value".
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
php finds the largest array Method of difference between value and minimum value
In PHP, you can use the max() and min() functions to find the maximum and minimum values of the array, and then combine the maximum and minimum values Subtract to find the difference.
Implementation steps:
Step 1: Use the max() function to obtain the maximum value of the array
max() function can Return the maximum value in an array
<?php header("Content-type:text/html;charset=utf-8"); $arr = array(2,3,4,5,67,8,32,1,23,45,0); var_dump($arr); $max=max($arr); echo "数组最大值为: ".$max; ?>
Step 2: Use the min() function to get the minimum value of the array
min() The function returns the minimum value in an array
$min=min($arr); echo "数组最小值为: ".$min;
Step 3: Use the "-" operator to subtract the obtained maximum value and minimum value to calculate the difference
最大值 - 最小值
Complete implementation example code:
<?php header("Content-type:text/html;charset=utf-8"); $arr = array(2,3,4,5,67,8,32,1,23,45,0); var_dump($arr); $max=max($arr); echo "数组最大值为: ".$max."<br>"; $min=min($arr); echo "数组最小值为: ".$min."<br>"; $res=$max-$min; echo "数组最大值和最小值的差值为:".$res; ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to find the difference between the maximum value and the minimum value of an array in php. For more information, please follow other related articles on the PHP Chinese website!