Jump to content

[SOLVED] Finding the mode


pea_1

Recommended Posts

made this just now... :D

 

<?php
function mode($array){
foreach($array as $k=>$v){
  $mode[trim($v)]++;
}
foreach($mode as $k=>$v){
  if($v>$highest[num]){
   $highest[num]=$v;
   $highest[word]=$k;
  }
}
return $highest[word];
}

$string='word, test, word, test2, word';
$array=explode(',',$string);
echo mode($array);
?>

Yeh it does. I also managed to get the top 5:

 

$excludes = array();

$i = 1;
while($i <= 5){
$i++;

$output = mode($array);

foreach($array as $lnum => $line){
array_push($excludes, trim($output));
foreach($excludes as $exclude){

if(trim($line) == $exclude){
unset($array[$lnum]);

}
}
}
echo "$output<br>";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.