Skip to content

Instantly share code, notes, and snippets.

@ischenkodv
Created December 23, 2009 23:33
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save ischenkodv/262906 to your computer and use it in GitHub Desktop.
Save ischenkodv/262906 to your computer and use it in GitHub Desktop.
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;
}
@mad-de
Copy link

mad-de commented Jun 24, 2016

hey! Im uusing your code in a small project of mine (https://github.com/mad-de/PlaCoTo/blob/master/site/resources/functions_calculate_placements.php). Is that alright with you and whats the best way to give you credit?

best wishes and thanks for your coding efforts!

@BramVanroy
Copy link

You probably want to sort your array before doing your median calculation.

@lwrbakro
Copy link

lwrbakro commented Aug 5, 2016

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

@pmichaelsen
Copy link

pmichaelsen commented Sep 16, 2016

There are some missing test cases:

<?php
$test = array(
    array(5,2,1,3,4),
    array(),
    array(1)
);
foreach($test as $arr){
    $count = count($arr);
    sort($arr);
    $mid = floor(($count-1)/2);
    $avg = ($arr)?array_sum($arr)/$count:0;
    $median = ($arr)?($arr[$mid]+$arr[$mid+1-$count%2])/2:0;
    echo 'avg: '.$avg."<br>";
    echo 'median: '.$median."<br>";
}
?>

Or maybe:

<?php
function median($arr){
    if($arr){
        $count = count($arr);
        sort($arr);
        $mid = floor(($count-1)/2);
        return ($arr[$mid]+$arr[$mid+1-$count%2])/2;
    }
    return 0;
}
function average($arr){
    return ($arr) ? array_sum($arr)/count($arr) : 0;
}
$test = array(
    array(5,2,1,3,4),
    array(5,4,2,3,1,6),
    array(),
    array(1),
    array(2,4),
); 
foreach($test as $arr){
    echo 'avg: '.average($arr)."<br>";
    echo 'median: '.median($arr)."<br>";
}
?>

@bishopb
Copy link

bishopb commented Oct 24, 2016

For the scenario where the data set has non-numeric keys (or no elements):

function median(array $arr)
{
    if (0 === count($arr)) {
        return null;
    }

    // sort the data
    $count = count($arr);
    asort($arr);

    // get the mid-point keys (1 or 2 of them)
    $mid  = floor(($count - 1) / 2);
    $keys = array_slice(array_keys($arr), $mid, (1 === $count % 2 ? 1 : 2));
    $sum  = 0;
    foreach ($keys as $key) {
        $sum += $arr[$key];
    }
    return $sum / count($keys);
}

@joshi1983
Copy link

These solutions are nicely understandable but not optimally efficient since they involve fully sorting the array before getting the median out of it.

The optimal efficiency for getting a median is actually O(n) not O(n log n). Have you tried implementing Hoare's Selection algorithm in PHP?
Someone else mentioned it at: http://stackoverflow.com/questions/4201292/on-algorithm-to-find-the-median-of-a-collection-of-numbers

@grenoult
Copy link

As BramVanroy and other mentioned, array needs to be sorted. In my case I used sort($arr);.

@steffanhalv
Copy link

Rewritten from javascript https://stackoverflow.com/a/45309555/2298745

Median PHP:

function median($values) {
  $count = count($values);
  if ($count === 0)  return null;
  asort($values);
  $half = floor($count / 2);
  if ($count % 2) return $values[$half];
  return ($values[$half - 1] + $values[$half]) / 2.0;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment