Calculating the median & average values of an array with PHP

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.

function calculate_median($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



You might like


Thanks, that helped.

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

Nice functions.

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

$total = array_sum($arr);


Hi Kris, thanks for the comment, that is true the way the function is written currently.

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.

There is one bug in the MEDIAN calculation. You must sort the array of values first. If you fail to do this, you will get all kinds of incorrect results.

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

Ah, true true Justin, if they aren't correctly sorted going in, that is a must. I will amend the function. Thanks.

to simplify the function further, you could write

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

so helpful. thank you so much.

Hi what about: write a function to find the avarage value of array W from low index to high index ? Thanks

Hi Matt,

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.

Thanks but PHP code should be shorter:

	$home_values_array = array("100000", "120000", "150000", "157000", "180000", "198000", "220000", "1450000");
	$m = count($home_values_array) / 2;
	$med = $m % 2 ? $home_values_array[$m] : ($home_values_array[$m] + $home_values_array[$m-1]) / 2;
	$avg = array_sum($home_values_array) / count($home_values_array);
	echo 'Median home value: $' . number_format($med) . '<br />'; 
	echo 'Average home value: $' . number_format($avg) . '<br />'; 

You should test if count($arr) is 0.

Almost 5 years later, and your post is still helping people like me. Thank you!

Haha, that's the beauty of the web right there Jim, and why I still keep paying for the hosting even though I have no time to write these days.

Merry Christmas

$count = count($arr);
$sum = array_sum($arr);
$average = ($sum/$count);



Average != Mean

Hi Brent,

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.


(optional, not publicly displayed) (optional)

Copyleft 2002 - 2016 Matt Jones
Hand crafted with HTML5 & CSS3
↑ Back to top