Finding the values in an array that have the most occurrences
arrays, count, php
Solution
use array_count_values
<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>
Array
(
[1] => 2
[hello] => 2
[world] => 1
)
http://php.net/manual/en/function.array-count-values.php
if you do not want the case-sensitive version use :
$ar = array_count_values(array_map('strtolower', $ar));
Problem
I have an array of tags taken from a MySQL database. Obviously, these tags are strings and are stored in an array, with one element in the array storing each tag. TO build my tag cloud, I want to be able to count the occurrences of each tag to find the most common tag. For example, if I have the table below... ``` tag1 tag1 hi bye gnu tux tag1 tux tux tag1 ... etc ``` ... the most commonly occurring tag is "tag1" what I want to do is count how many times that tag occurs in the array. `Max()` doesn't help here due to it only liking numerical values.