Does anybody know a simple way to retrieve the largest value (and that value's key name) from an associative array?
Basically what I've got is a database of categorized articles. Here's what I've tried so far:
$categories;
while($item = mysql_fetch_array($result, MYSQL_NUM))
{
$categories["$item[0]"]++;
}
$max[0] = 0;
$max[1] = "category";
foreach($categories as $item)
{
if($item > $max[0])
{
$max[0] = $item;
$max[1] = current($categories);
}
}
echo "{$max[0]} {$max[1]}<br>";
$max[0] outputs the correct integer, but $max[1] also outputs an integer instead of a string. I assume that
current($categories)
is wrong - what should I use instead to get that key's name?
Thanks
Steve