tags:

views:

29

answers:

1

I am trying to set lower limit and upper limit for an array. The lower limit is always constant, which is 0 but i want the upper limit to be varying based on the array values.

some times the array values are 1,2,3,4,5 and sometimes the array values are 100,200,300,400,500 and sometimes in thousands as well. So i do not want to set a constant value for upper limit as max($array) + 100 or something like that... so what could can i apply to make it variable according to the array values. I want the upper limit to be comfortably more than max of $array but not way too more or way too less. Any Suggestions?

+1  A: 
$max = (array_sum($array) / sizeof($array)) + max($array)

Examples:

function getMax($array){
    return (array_sum($array) / sizeof($array)) + max($array);
}

$array = Array(1, 2, 3, 4, 5);

echo getMax($array) . '<br/>';

$array = Array(100, 200, 300, 400, 500);

echo getMax($array) . '<br/>';

$array = Array(1123212, 2143212, 8323212, 7223212, 1023212);

echo getMax($array) . '<br/>';

Output:

8 
800 
12290424

So $max would never be more than array_max($array) + the average of the array values.

Michael Robinson
Thank you for your effort Michael :)
Scorpion King
Glad I could help :)
Michael Robinson