H-Fish Posted August 22, 2007 Share Posted August 22, 2007 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 More sharing options...
trq Posted August 22, 2007 Share Posted August 22, 2007 Take a look at array_count_values(). Link to comment https://forums.phpfreaks.com/topic/66184-solved-same-contents-in-array/#findComment-331033 Share on other sites More sharing options...
H-Fish Posted August 22, 2007 Author Share Posted August 22, 2007 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 Link to comment https://forums.phpfreaks.com/topic/66184-solved-same-contents-in-array/#findComment-331055 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.