Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bendo01/a406c22f33578230aa83bcf5a34f718b to your computer and use it in GitHub Desktop.
Save bendo01/a406c22f33578230aa83bcf5a34f718b 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment