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


You might like:

CommentsComments RSS


Rod's Avatar

Thanks, that helped.

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

Kris's Avatar

Nice functions.

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

$total = array_sum($arr);

Thanks!

Matt's Avatar

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.

Justin Noel's Avatar

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.

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

Matt's Avatar

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

nathan's Avatar

to simplify the function further, you could write

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

mary's Avatar

so helpful. thank you so much.

An's Avatar

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

Stuart's Avatar

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.

Stanislav Terleckiy's Avatar

Thanks but PHP code should be shorter:

<?php
	$home_values_array = array("100000", "120000", "150000", "157000", "180000", "198000", "220000", "1450000");
	sort($home_values_array);
	$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 />'; 
?>


(optional, not publicly displayed) (optional)

Privacy Policy | About | Contact
Copyleft 2002 - 2013 Matt Jones
Hand crafted with HTML5 & CSS3
↑ Back to top