Jump to content

Recommended Posts

Hi there, i have an array that i have created by exploding a string that is comma seperated

(for example my string was 1,2,3,4,5,6,7,8,9 and now i have this as array[0] = 1 etc etc)

 

What i am wondering, is if my string was more like 1,2,3,5,6,7,1,1,1,8,9,3,3,2,2 could i search the array and find what appears most frequently in the values (in the above case 1)?

 

So if 1 has 4 occurences, then i echo this value first, then 3 which has 3 occurrences (etc etc)

 

I am trying to make a basic tagging system for my website, and i want the tags that appear most to be displayed first or in a bigger font or something :)

Link to comment
https://forums.phpfreaks.com/topic/66184-solved-same-contents-in-array/
Share on other sites

Thanks, i cracked it this way:

function array_repeated($array) {

    if(!is_array($array)) return false;
    $repeated_values = Array();
    $array_unique = array_unique($array);
    if(count($array)-count($array_unique)) {
        for($i=0;$i<count($array);$i++) {
            if(!array_key_exists($i, $array_unique)) $repeated_values[] = $array[$i];
        }
    }
    return $repeated_values;
}
$repeatedtags = array_repeated($tags); // get the most repeated values from the array tags into a new array
// if there wasn't any repeated just use the old array with the new name
if (empty($repeatedtags))
{
$repeatedtags = $tags;
}
// set up some randomness
srand((float) microtime() * 10000000);
$rand_keys = array_rand($tags, 2);
$rand_keys2 = array_rand($repeatedtags, 2);


echo "$repeatedtags[0]"; // the first most popular tag
echo "$repeatedtags[1]"; // second most popular tag
echo $repeatedtags[$rand_keys2[0]]; // random popular tag
echo $repeatedtags[$rand_keys2[1]]; // random popular tag
echo "$repeatedtags[4]"; // 5th most popular tag
echo $tags[$rand_keys[0]]; // random tag
echo $tags[$rand_keys[1]]; // random tag

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.