note 95102 deleted from function.array-sum by danbrown
| From: | danbrown@php.net | Date: | Mon, 14 Dec 2009 16:00:49 +0000 |
| Subject: | note 95102 deleted from function.array-sum by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-163941@lists.php.net to get a copy of this message | ||
Note Submitter: Tim White
----
In reference to jodymickey's array_mode function...
This function returns the mode(s) of an array (and not the number of times the value actually
occured).
An array is always the returned result.
This function does *not* suffer from the same problem in that if there is more than one mode, it
will return only one.
<?php
/**
* array_mode
*
* @param array $list
* @static
* @access public
* @return array
*/
public static function array_mode(array $list)
{
$list = array_count_values($list);
asort($list, SORT_NUMERIC);
$result = array();
end($list);
while (! is_null($key = key($list)) ) {
if (isset($current) && ($current > current($list))) break;
$current = current($list);
$result[] = $key;
prev($list);
}
return $result;
}
?>