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);
?>

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.