johnsmith153 Posted August 29, 2008 Share Posted August 29, 2008 I have the following info in an array: Jim, Bob, Dave, Jim, Jim, Jim, Paul, Jim As you can see above - Jim has 5 entries I need to sort it so I can check for the name and number of occurences of the 1st, 2nd,3rd most popular. I know array_count_values can do sort of this - but as far as I am aware you need to know the value you are searching (i.e Jim) Assume I dont know the expected value - all I know is that I want to return the one with the most occurences. Link to comment https://forums.phpfreaks.com/topic/121925-quick-question-about-array/ Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 <?php $array = array('jim', 'bob', 'dave', 'jim', 'jim', 'jim', 'paul', 'jim'); $nums = array_count_values($array); sort($nums, SORT_NUMERIC); $sorted = array_slice($nums, 0, 3); foreach ($sorted as $name=>$count) { echo "$name appeared $count times"; } Link to comment https://forums.phpfreaks.com/topic/121925-quick-question-about-array/#findComment-629231 Share on other sites More sharing options...
johnsmith153 Posted August 29, 2008 Author Share Posted August 29, 2008 OUTSTANDING - quick response. however, it doesn't work. I can see it can work, must be something slightly out: I get this returned: 0 appeared 1 times1 appeared 1 times2 appeared 1 times I will try to modify, see if I can beat DarkWater to the solution! Link to comment https://forums.phpfreaks.com/topic/121925-quick-question-about-array/#findComment-629241 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 Duh, forgot about keys being reset by sort() and the way it sorts. Use arsort() instead, with the same flags. <?php $array = array('jim', 'bob', 'dave', 'jim', 'jim', 'jim', 'paul', 'jim'); $nums = array_count_values($array); arsort($nums, SORT_NUMERIC); $sorted = array_slice($nums, 0, 3); foreach ($sorted as $name=>$count) { echo "$name appeared $count times<br />\n"; } Link to comment https://forums.phpfreaks.com/topic/121925-quick-question-about-array/#findComment-629245 Share on other sites More sharing options...
johnsmith153 Posted August 29, 2008 Author Share Posted August 29, 2008 I actually gave up and kept pressing my refresh button. Quicker that way. Link to comment https://forums.phpfreaks.com/topic/121925-quick-question-about-array/#findComment-629246 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 LOL Link to comment https://forums.phpfreaks.com/topic/121925-quick-question-about-array/#findComment-629247 Share on other sites More sharing options...
DarkWater Posted August 29, 2008 Share Posted August 29, 2008 Well, I tested the code and it worked, so there you go. But that's still kinda funny. Link to comment https://forums.phpfreaks.com/topic/121925-quick-question-about-array/#findComment-629251 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.