Calculates and returns the mode of the numbers given
<?php function mode($numbers=array()) { if (!is_array($numbers)) $numbers = func_get_args(); $amt = array_count_values($numbers); sort($amt); foreach ($amt as $key => $val) if ($val == max($amt)) return $key; } /* echo mode(array(2,2,3,3,4,5)); // 2 echo mode(4,4,3,3,3,2,4,5); // 4 */ ?>
As seen in the commented portion of code, this function can take either an array of numbers, or multiple parameters of numbers. *note* returns the "first" key that is a mode, only one is returned regardless of uniqueness.
Comments
Add your comment