/    Sign up×
Community /Pin to ProfileBookmark

Find the most common value in an array….

Is there a way to find the most commonly occuring value in an array?

For example, if $array[0] = “Red” / $array[1] = “Blue” / $array[2] = “Red” / $array[3] = “Yellow” how would I return that “Red” is the most common value? I made some big complicated routine but I got errors like crazy.

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 16.2005 — [code=php]
<?php
# returns most common value in input array
function array_most_common($input)
{
$counted = array_count_values($input);
arsort($counted);
return(key($counted));
}
[/code]
Copy linkTweet thisAlerts:
@abudabitauthorMar 16.2005 — Wow! That's about 1/5th the code I came up with. Didn't know how to use array_count_values.
Copy linkTweet thisAlerts:
@NogDogMar 17.2005 — [i]Originally posted by abudabit [/i]

[B]Wow! That's about 1/5th the code I came up with. Didn't know how to use array_count_values. [/B][/QUOTE]

Yeah, I started thinking about how to do it, had a bunch of lines of code, then I thought, "Hey, let's see what array functions PHP has that might make this easier." As usual, I found some. ?
Copy linkTweet thisAlerts:
@aadiJul 22.2010 — [code=php]$res=mysql_query("select * FROM check");
while($row=mysql_fetch_array($res)){
$input[]=$row['value'];
}
# returns most common value in input array
function array_most_common($input)
{
$counted = array_count_values($input);
arsort($counted);
return(key($counted));

} [/code]


where i can echo most common value.....?

and 1 want sort by most common
Copy linkTweet thisAlerts:
@NogDogJul 22.2010 — [code=php]$res=mysql_query("select * FROM check");
while($row=mysql_fetch_array($res)){
$input[]=$row['value'];
}
# returns most common value in input array
function array_most_common($input)
{
$counted = array_count_values($input);
arsort($counted);
return(key($counted));

} [/code]


where i can echo most common value.....?

and 1 want sort by most common[/QUOTE]


[code=php]
echo array_most_common($input);
[/code]

(After you finish populating $input)
Copy linkTweet thisAlerts:
@anejoblancoJul 19.2011 — Breaking out the Necronomicon as this thread answered my question perfectly. How would I return the top 10 results?
Copy linkTweet thisAlerts:
@NogDogJul 19.2011 — Breaking out the Necronomicon as this thread answered my question perfectly. How would I return the top 10 results?[/QUOTE]

After doing the arsort(), use array_slice() to grab the first 10 array elements.
Copy linkTweet thisAlerts:
@anejoblancoJul 19.2011 — Thanks for the reply, its doing what I wanted it to do ... sort of. I've adjusted the code so that the variable $top_ten is the results of array_slice, and then to return that variable. The problem is when I then use echo to print out those results it simply prints out "Array".

[code=php]
function array_most_common($array)
{
$counted = array_count_values($array);
arsort($counted);
$top_ten = array_slice($counted, 0, 9);
return ($top_ten);

}

echo array_most_common($array);

[/code]


I have also tried it this way...

[code=php]
function array_most_common($array)
{
$counted = array_count_values($array);
arsort($counted);
array_slice($counted, 0, 9);
return (key($counted));

}

echo array_most_common($array);
[/code]


That only returns the first value in position 0 regardless of what I point it at as the start position, if I remove key it once again returns "Array". I'm not sure what I am doing wrong here for it to just return the word array I must be missing out a vital step.

Might also be worth mentioning (probably not but this is what i'm finding), If I have array_slice put its results into a variable like in the first example, then whilst it only returns one value from my array it does return the correct value from the position i tell it to start at.
Copy linkTweet thisAlerts:
@NogDogJul 19.2011 — Since you'll be dealing with an array of the top ten, you'll have to either to a foreach() on the result, or possibly an implode() in order to output the results.
[code=php]
<?php
function array_most_common($array)
{
$counted = array_count_values($array);
arsort($counted);
$top_ten = array_slice($counted, 0, 9);
return ($top_ten);

}
$mostCommon = array_most_common($array);
?>
<table>
<tr><th>Item</th><th>Count</th></tr>
<?php
foreach($mostCommon as $item => $count) {
echo "<tr><td>$item</td><td>$count</td></tr>n";
}
?>
</table>
[/code]
Copy linkTweet thisAlerts:
@anejoblancoJul 19.2011 — Aha thank you, I had a feeling that I had to use a foreach loop but I was using it in the completely wrong place, now seeing it in the right place makes understanding its usage a lot easier, thanks again.
×

Success!

Help @abudabit spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...