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 Link to comment https://forums.phpfreaks.com/topic/53721-solved-finding-the-mode/ 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 Link to comment https://forums.phpfreaks.com/topic/53721-solved-finding-the-mode/#findComment-265473 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 Link to comment https://forums.phpfreaks.com/topic/53721-solved-finding-the-mode/#findComment-265477 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. Link to comment https://forums.phpfreaks.com/topic/53721-solved-finding-the-mode/#findComment-265480 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 Link to comment https://forums.phpfreaks.com/topic/53721-solved-finding-the-mode/#findComment-265484 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. Link to comment https://forums.phpfreaks.com/topic/53721-solved-finding-the-mode/#findComment-265492 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 Link to comment https://forums.phpfreaks.com/topic/53721-solved-finding-the-mode/#findComment-265502 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); ?> Link to comment https://forums.phpfreaks.com/topic/53721-solved-finding-the-mode/#findComment-265527 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. Link to comment https://forums.phpfreaks.com/topic/53721-solved-finding-the-mode/#findComment-265529 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. Link to comment https://forums.phpfreaks.com/topic/53721-solved-finding-the-mode/#findComment-265635 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>"; Link to comment https://forums.phpfreaks.com/topic/53721-solved-finding-the-mode/#findComment-265642 Share on other sites More sharing options...
Lumio Posted May 31, 2007 Share Posted May 31, 2007 Please click on topic solved Link to comment https://forums.phpfreaks.com/topic/53721-solved-finding-the-mode/#findComment-265788 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.