How can I sum the entire refund amount after getting the maximum value from the array?

I did not dare to answer this question, or I should ask or not. I think this is a much more logical question. But I can’t figure it out.

I have an array that can include

$programFees = [100,200,100,500,800,800]

I can get the maximum value from this array with max($programFees). If I have 2 max values, for example 800,800, I want to take only one. I realized that php maxcan solve this problem.

But I want to sum the entire remaining amount from this array.

eg

$maxProgramFees = max($programFees); 
//I got 800 and remaining amount is [100,200,100,500,800]

My total amount should be 1700. What approach should I use to get this amount?

+4
source share
2 answers

, , , .

<?php 
$programFees = [100, 200, 100, 500, 800, 800];
rsort($programFees); //sort high -> low

$highest = $programFees[0]; // 800
array_shift($programFees); // remove the highest value

$sum = array_sum($programFees); // 1700

rsort()

array_shift()

array_sum()

+5

@helllomatt , , - , - .

$programFees = [100,200,100,500,800,800];
$maxProgramFees = max($programFees);
$sum = array_sum(array_diff($programFees, array($maxProgramFees)));

, rsort() .

+1

Source: https://habr.com/ru/post/1690361/


All Articles