PHP: Calculating the median value.

In this tutorial, we are going to show you how to calculate a median value using PHP.

As you probably already know, the median value is essentially the middle number in a set of numbers.

In our case, this set of numbers will be a PHP array.

If you are looking for a PHP function that you can easily 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 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 will need to be sorted before you pass it into the function. This function will not sort the array for you because some sets of numbers can be in descending order. As a result, you will need to use the sort function or the rsort function beforehand.
  • 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 description of our median function.

In our getMedian function:

  1. We 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 count 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 was 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 presume that the middle number is the “lower” end of the middle.
  8. To work out the median of an odd-sized array, we calculate the average of the two middle numbers.

Examples.

Here are a few 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!