chopWood Posted December 23, 2007 Share Posted December 23, 2007 If I have the following array: $myArray(jim,ed,gary,jim,jim,fred,gary,alice,mary, alice,alice); What's a simple way to get a total on each unique value, such as: jim=3 ed=1 gary=2 alice=3 mary=1 fred=1 thanks Quote Link to comment https://forums.phpfreaks.com/topic/82958-counting-duplicate-array-items/ Share on other sites More sharing options...
papaface Posted December 23, 2007 Share Posted December 23, 2007 http://uk.php.net/manual/en/function.array-count-values.php ? Quote Link to comment https://forums.phpfreaks.com/topic/82958-counting-duplicate-array-items/#findComment-421929 Share on other sites More sharing options...
corbin Posted December 23, 2007 Share Posted December 23, 2007 Hrmmm this would be a ghetto way to do it, and depending on the values, it could cause problems with the values -> keys messing up data, but anyway: $orig_array = array('corbin', 'corbin', 'john', 'mark', 'mark', 'paul', 'paul', 'mary', 'mary', 'mary'); $count_array = array(); foreach($orig_array as $v) { (isset($count_array[$v])) ? $count_array[$v]++ : $count_array[$v] = 1; } print_r($count_array); /* Array ( [corbin] => 2 [john] => 1 [mark] => 2 [paul] => 2 [mary] => 3 ) */ Edt: whoa.... Hadn't heard of array_count_values before.... Already typed this out though, so posting it anyway ;p. Quote Link to comment https://forums.phpfreaks.com/topic/82958-counting-duplicate-array-items/#findComment-421930 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.