The following mode function will return the most commonly occurring value from an array of values, also called the mode. If just the array is used then only the most commonly occurring value will be returned. The second parameter can be used to return an array containing the mode and the number of times that this value occurs in the array.
function array_mode($array,$justMode=0) { foreach ( $array as $item) { $count[$item]++; }else{ $count[$item] = 1; }; }; $mostcommon = ''; $iter = 0; foreach ( $count as $k => $v ) { if ( $v > $iter ) { $mostcommon = $k; $iter = $v; }; }; if ( $justMode==0 ) { return $mostcommon; } else { } }
This is used in the following way.
If you just wanted a list of the most commonly occurring values in an array then you could use the PHP function array_count_values(). This will return an array containing all of the values in the array and the number of times each one occurs. Here is an example of it's use.
0 comments:
Post a Comment