Home  >  Article  >  Backend Development  >  How to find the maximum difference of an array in php

How to find the maximum difference of an array in php

青灯夜游
青灯夜游Original
2022-08-18 18:38:151635browse

Steps to find the maximum difference of an array: 1. Use the max() function to get the maximum value of the array, the syntax is "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 of the array to calculate the maximum difference of the array. The syntax is "maximum value - minimum value".

How to find the maximum difference of an array in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

Method to find the maximum difference of an array : The maximum value of the array minus the minimum value of the array

In PHP, you can use the max(), min() functions and the "-" operator to find the maximum difference of the array.

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); 
var_dump($arr);

$max=max($arr);
echo "数组最大值为: ".$max;

?>

How to find the maximum difference of an array in php

Step 2: Use the min() function to get the minimum value of the array

min() The function returns the minimum 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); 
var_dump($arr);
$max=max($arr);
echo "数组最大值为: ".$max."<br>";

$min=min($arr);
echo "数组最小值为: ".$min."<br>";

?>

How to find the maximum difference of an array in php

Step 3: Use the "-" operator to subtract the obtained maximum value and minimum value to calculate the maximum difference

<?php
header("Content-type:text/html;charset=utf-8");
$arr = array(2,3,4,5,67,8,32,1,23,45); 
var_dump($arr);
$max=max($arr);
echo "数组最大值为: ".$max."<br>";
$min=min($arr);
echo "数组最小值为: ".$min."<br>";

$res=$max-$min;
echo "数组的最大差值为:".$res;
?>

How to find the maximum difference of an array in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to find the maximum difference of an array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn