abch624 Posted July 6, 2008 Share Posted July 6, 2008 Hi guys a quick one... I have an array with strings in it and I would like to get out the number of times on string has been repeated in the array.... foreach ($tags as $key => $value) { } I would like to know how many time this "$key" has been repeated in the array. Please help Quote Link to comment https://forums.phpfreaks.com/topic/113487-array-function/ Share on other sites More sharing options...
DarkWater Posted July 6, 2008 Share Posted July 6, 2008 You can use: $counts = array_count_values(array_keys($tags)); The values of $counts will correspond to the frequency of the key of the array, which corresponds to the keys of the first array. Quote Link to comment https://forums.phpfreaks.com/topic/113487-array-function/#findComment-583122 Share on other sites More sharing options...
abch624 Posted July 6, 2008 Author Share Posted July 6, 2008 na that didnt work just gives me one for all Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1 [5] => 1 [6] => 1 [7] => 1 [8] => 1 [9] => 1 [10] => 1 [11] => 1 [12] => 1 [13] => 1 [14] => 1 [15] => 1 [16] => 1 [17] => 1 [18] => 1 [19] => 1 ) Quote Link to comment https://forums.phpfreaks.com/topic/113487-array-function/#findComment-583131 Share on other sites More sharing options...
Barand Posted July 6, 2008 Share Posted July 6, 2008 A key value can only occur once in an array Quote Link to comment https://forums.phpfreaks.com/topic/113487-array-function/#findComment-583139 Share on other sites More sharing options...
DarkWater Posted July 7, 2008 Share Posted July 7, 2008 WOW, I didn't think of that. Good catch, Barand. If array keys could appear more than once, my code would have worked, lol. Do you want the counts of duplicate array VALUES? Quote Link to comment https://forums.phpfreaks.com/topic/113487-array-function/#findComment-583166 Share on other sites More sharing options...
abch624 Posted July 7, 2008 Author Share Posted July 7, 2008 Yes I said repeated strings lol help guys Quote Link to comment https://forums.phpfreaks.com/topic/113487-array-function/#findComment-583167 Share on other sites More sharing options...
CMC Posted July 7, 2008 Share Posted July 7, 2008 I would like to know how many time this "$key" has been repeated in the array. Please help as Barand helpfully pointed out: A key value can only occur once in an array Maybe you want repeated values. $array = array("apple" => "green", "apple" => "red", "strawberry" => "red" ); Are you trying to see how many times apple is in the array, or red? apple = key red = value Quote Link to comment https://forums.phpfreaks.com/topic/113487-array-function/#findComment-583230 Share on other sites More sharing options...
Barand Posted July 7, 2008 Share Posted July 7, 2008 try <?php $tags = array ('A', 'B', 'A', 'C', 'D', 'B', 'A', 'C', 'A', 'C', 'D', 'E'); $tagCounts = array_count_values($tags); foreach ($tags as $k => $v) { echo "$k : $v (occurs {$tagCounts[$v]} times)<br/>"; } ?> --> 0 : A (occurs 4 times) 1 : B (occurs 2 times) 2 : A (occurs 4 times) 3 : C (occurs 3 times) 4 : D (occurs 2 times) 5 : B (occurs 2 times) 6 : A (occurs 4 times) 7 : C (occurs 3 times) 8 : A (occurs 4 times) 9 : C (occurs 3 times) 10 : D (occurs 2 times) 11 : E (occurs 1 times) Quote Link to comment https://forums.phpfreaks.com/topic/113487-array-function/#findComment-583331 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.