Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prmichaelsen/685236b4ec4d321a037f36debed1e15c to your computer and use it in GitHub Desktop.
Save prmichaelsen/685236b4ec4d321a037f36debed1e15c to your computer and use it in GitHub Desktop.
function median($arr){
if($arr){
$count = count($arr);
sort($arr);
$mid = floor($count/2);
return ($arr[$mid]+$arr[$mid+1-$count%2])/2;
}
return false;
}
function average($arr){
return ($arr) ? array_sum($arr)/count($arr) : false;
}
//test it out
$test = array(
array(5,2,1,3,4),
array(),
array(1)
);
foreach($test as $arr){
echo 'avg: '.average($arr)."<br>";
echo 'median: '.median($arr)."<br>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment