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

Share or Bookmark This Post:


Comments

RSS Comments Feed


Rod's Avatar

Rod

Thanks, that helped.

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

Kris's Avatar

Kris

Nice functions.

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

$total = array_sum($arr);

Thanks!

Matt's Avatar

Matt

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

Justin Noel

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

Matt

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

nathan

to simplify the function further, you could write

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

mary's Avatar

mary

so helpful. thank you so much.




(optional, not publicly displayed)


(optional)

Subscribe

RSS Feed

Archives

Powered by HTML5

HTML5 Powered with CSS3 / Styling, Multimedia, and Semantics