note 13793 added to function.array.php
| From: | eric at genscape dot com | Date: | Tue, 03 Jul 2001 16:20:08 +0000 |
| Subject: | note 13793 added to function.array.php | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-14938@lists.php.net to get a copy of this message | ||
I was needing to find the mathematical mode of a list of numbers. Here's a quick function I
threw together:
function array_mode($haystack) {
for($i=0;$i<sizeof($haystack);$i++) {
$needle = $haystack[$i];
$count[$needle]++;
}
arsort($count,SORT_NUMERIC);
while (list ($key, $val) = each ($count)) {
return $key;
}
}
Anyone know of an easier or better way of doing this? I think this works, but I haven't done
too much testing.
http://www.php.net/manual/en/function.array.php