Home >Backend Development >PHP Problem >How to add two maximum values ​​in php

How to add two maximum values ​​in php

青灯夜游
青灯夜游Original
2022-05-31 15:05:572156browse

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]".

How to add two maximum values ​​in php

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);
?>

How to add two maximum values ​​in php

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;

How to add two maximum values ​​in php

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!

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