pea_1 Posted May 31, 2007 Share Posted May 31, 2007 Can't figure out how to find the mode of a file (string occurring most often). Has anyone else managed to do it? Peter Quote Link to comment Share on other sites More sharing options...
Lumio Posted May 31, 2007 Share Posted May 31, 2007 If you mean fileperms: www.php.net/fileperms www.php.net/is_readable www.php.net/is_writeable Quote Link to comment Share on other sites More sharing options...
pea_1 Posted May 31, 2007 Author Share Posted May 31, 2007 no the mathematical mode. the mode is the most often value. For example, for 1,2,2,2,3 2 would be the mode Quote Link to comment Share on other sites More sharing options...
Orio Posted May 31, 2007 Share Posted May 31, 2007 I really don't understand what you are looking for... Are you sure it's not fileperms() you need? Orio. Quote Link to comment Share on other sites More sharing options...
pea_1 Posted May 31, 2007 Author Share Posted May 31, 2007 Umm.. this is *extremely* simple maths. Maybe this will help you http://www.manatee.k12.fl.us/sites/elementary/palmasola/mathlabtutstat1.htm Quote Link to comment Share on other sites More sharing options...
Orio Posted May 31, 2007 Share Posted May 31, 2007 Oh ok lol I assume the numbers are in an array? <?php //The numbers are in an array called $array $count = array_count_values($array); sort($count); echo $count[0]; ?> Orio. Quote Link to comment Share on other sites More sharing options...
pea_1 Posted May 31, 2007 Author Share Posted May 31, 2007 The array doesn't contain numbers though, they're words Quote Link to comment Share on other sites More sharing options...
taith Posted May 31, 2007 Share Posted May 31, 2007 made this just now... <?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); ?> Quote Link to comment Share on other sites More sharing options...
pea_1 Posted May 31, 2007 Author Share Posted May 31, 2007 Ah thanks, i was completely lost on this one, but this seems to do the trick. Quote Link to comment Share on other sites More sharing options...
Orio Posted May 31, 2007 Share Posted May 31, 2007 Btw, the code I supplied works with words too, it just needs a little change (although it might work this way too). Orio. Quote Link to comment Share on other sites More sharing options...
pea_1 Posted May 31, 2007 Author Share Posted May 31, 2007 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>"; Quote Link to comment Share on other sites More sharing options...
Lumio Posted May 31, 2007 Share Posted May 31, 2007 Please click on topic solved Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.