# Calculating the median & average values of an array with PHP

Here's a couple of handy little PHP functions I wrote to calculate the median & average values of an array of numbers.

The median value is literally the middle number, this is useful in many instances, often used in real estate statistics for example, as it can help show what people are paying for a home, without the value being skewed by a few low-end or high-end transactions.

Take the following home sales numbers for example;

- $100,000
- $120,000
- $150,000
- $157,000
- $180,000
- $198,000
- $220,000
- $1,450,000

The average of those home sale prices would be $321,875, we can see that the high-end home price has dramatically skewed the true price most people are paying. Now using the median calculation, we can determine that the median price would be $168,500. The functions below don't do any error checking, so you may want to add checks to verify an array is being passed, whether the values are numeric, etc.

<?php function calculate_median($arr) { sort($arr); $count = count($arr); //total numbers in array $middleval = floor(($count-1)/2); // find the middle value, or the lowest middle value if($count % 2) { // odd number, middle is the median $median = $arr[$middleval]; } else { // even number, calculate avg of 2 medians $low = $arr[$middleval]; $high = $arr[$middleval+1]; $median = (($low+$high)/2); } return $median; } function calculate_average($arr) { $count = count($arr); //total numbers in array foreach ($arr as $value) { $total = $total + $value; // total value of array numbers } $average = ($total/$count); // get average value return $average; } $home_values_array = array("100000", "120000", "150000", "157000", "180000", "198000", "220000", "1450000"); $median_home_value = calculate_median($home_values_array); echo '<p>Median home value: $'.number_format($median_home_value).'<br />'; $average_home_value = calculate_average($home_values_array); echo 'Average home value: $'.number_format($average_home_value).'</p>'; ?>

The resulting output from the above would be;

Median home value: $168,500

Average home value: $321,875

## Comments

How come php don't have a native avg function while js does?

They've got min: http://www.php.net/manual/en/function.min.php

and max: http://www.php.net/manual/en/function.max.php

but no avg... go figure.

In the calculate_average function, you can replace the foreach loop with this:

$total = array_sum($arr);

Thanks!

The primary reason I used a loop was so you can mess around with each array element; i.e. check with is_numeric or something along those lines.

...

sort($arr);

$count = count($arr); //total numbers in array

...

function calculate_average($arr)

{

return array_sum($arr)/count($arr)

}

Wouldn't using asort in the above function do what you're asking?

http://www.php.net/manual/en/function.asor...

I'm looking at using your functions, however I have a multidimensional array, what would be the easiest way to do this? Pull out the values from the multidimensional array that I want to use and place it into their own array? I can see some issues with this, but not 100% sure which way to go.

Merry Christmas

$sum = array_sum($arr);

$average = ($sum/$count);

success

Average != Mean

Unless they changed something in mathematics recently, average is just a synonym for the arithmetic mean, which is the value obtained by dividing the sum of the set of quantities by the number of quantities in the set.

--Matt