PHP: Calculating the median value.

This is a short guide on how to calculate the median using PHP. As you probably already know, the median value is essentially the middle number of a set of numbers. In our case, this set of numbers will be a PHP array.

For those of you who just want a quick PHP function that you can copy and paste into your project, you can check out the code below:

/**
 * A PHP function that will calculate the median value
 * of an array
 * 
 * @param array $arr The array that you want to get the median value of.
 * @return boolean|float|int
 * @throws Exception If it's not an array
 */
function getMedian($arr) {
    //Make sure it's an array.
    if(!is_array($arr)){
        throw new Exception('$arr must be an array!');
    }
    //If it's an empty array, return FALSE.
    if(empty($arr)){
        return false;
    }
    //Count how many elements are in the array.
    $num = count($arr);
    //Determine the middle value of the array.
    $middleVal = floor(($num - 1) / 2);
    //If the size of the array is an odd number,
    //then the middle value is the median.
    if($num % 2) { 
        return $arr[$middleVal];
    } 
    //If the size of the array is an even number, then we
    //have to get the two middle values and get their
    //average
    else {
        //The $middleVal var will be the low
        //end of the middle
        $lowMid = $arr[$middleVal];
        $highMid = $arr[$middleVal + 1];
        //Return the average of the low and high.
        return (($lowMid + $highMid) / 2);
    }
}

The handy PHP function above takes in an array of numbers and returns the median value. However, there are two very important things to note here:

  • Firstly, your array should be sorted before you pass it into the function. The function does not sort the array for you because some sets of numbers may be in a descending order. As a result, you will need to use either the sort function or the rsort function.
  • Secondly, associative arrays will not work. If you have an associative array, then you will need to copy the values over into an array that has numerical indexes.

A drill-down of our median function.

In our getMedian function, we:

  1. Checked to see if the parameter was an array or not. If it is not a valid array, we throw an exception.
  2. If the array is empty, then we return a boolean FALSE value. This is because you cannot get the median value of an empty set of numbers.
  3. We then calculated how many numbers are in our array.
  4. We calculated the “middle number” of the set by getting the middle index of the array.
  5. After that, we checked to see if the size of the array is even or uneven.
  6. If the size of the array is uneven, we simply return the middle number. There is no need for any further calculations.
  7. However, if the size of the array is odd, then we assume that the middle number is the “lower” end of the middle.
  8. To work out the median of an odd-sized array, we have to take the two middle numbers and get the average of them.

Examples.

Examples of this PHP function in use:

//Odd-numbered array.
$arr = array(1, 2, 3, 4, 5);
var_dump(getMedian($arr)); //Result: 3

//Even-numbered array.
$arr = array(1, 2, 3, 4, 5, 6);
var_dump(getMedian($arr)); //Result: 3.5

//A more varied array.
$arr = array(1, 4, 6, 19, 24, 90);
var_dump(getMedian($arr)); //Result: 12.5

Try it out for yourself!