Home > Article > Backend Development > How to add two maximum values in php
Implementation method: 1. Store the data in the array, the syntax "$arr=array(value 1, value 2..);"; 2. Use rsort() to sort the array in descending order, the syntax " rsort($arr)"; 3. Add the first element and the second element of the descending array, the syntax is "$arr[0] $arr[1]".
The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer
In php, I want to display a set of data , to find two maximum values and add the two maximum values, you can use array.
You only need to store the data into the array, and then sort the array in descending order, so that the first element and the second element of the array are the two maximum values, and you can add them together.
Step 1. Store the data in the array
$arr=array(3,9,32,4,1,12,45,8);
Step 2. Use the rsort() function to sort in descending order
## The #rsort() function can sort a numerical array in descending order.<?php $arr=array(3,9,32,4,1,12,45,8); var_dump($arr); rsort($arr); var_dump($arr); ?>It can be seen that the first element and the second element of the array are the two maximum values. You only need to use the array subscript to take them out.
$arr[0] //数组第一个元素 $arr[0] //数组第二个元素
Step 3. Add the first element and the second element x
$res=$arr[0]+$arr[1]; echo "两个最大值相加:".$arr[0]." + ".$arr[1]." = ".$res;Recommended learning : "
PHP Video Tutorial"
The above is the detailed content of How to add two maximum values in php. For more information, please follow other related articles on the PHP Chinese website!