string - PHP how to display the 5 most reccurent words in a list -
i have list in visited websites stored (a thousand), , need display top 5 visited websites :
$websites= "site#1.com site#2.com site#1.com site#1.com site#3.com ... "
this deliberately verbose understand what's going on:
<?php $websites = "site#1.com site#2.com site#1.com site#1.com site#3.com"; //presuming they'll seperated single space... $sites = explode(' ', $websites); $sitecount = array(); foreach ($sites $site) { if (!isset($sitecount[$site])) { $sitecount[$site] = 1; } else { $sitecount[$site]++; } } arsort($sitecount); $finalarray = array_slice($sitecount, 0, 5); var_dump($sitecount);
which outputs:
array(3) { ["site#1.com"]=> int(3) ["site#3.com"]=> int(1) ["site#2.com"]=> int(1) }
Comments
Post a Comment