musclehead Posted January 29, 2008 Share Posted January 29, 2008 Hello, I have a simple, one-dimensional array: $myArray = array('apple','apple','orange','apple','banana','orange','orange','orange','banana','apple'); I'm looking for an easy way/built-in function to count the distinct values in that array, so to return 4 for apples. Any ideas? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/88388-solved-count-distinct-values-in-array/ Share on other sites More sharing options...
p2grace Posted January 29, 2008 Share Posted January 29, 2008 If you don't need to know how many of a specific item there is you could use this. $myArray = array('apple','apple','orange','apple','banana','orange','orange','orange','banana','apple'); $myArray = array_unique($myArray); $count = count($myArray); Quote Link to comment https://forums.phpfreaks.com/topic/88388-solved-count-distinct-values-in-array/#findComment-452370 Share on other sites More sharing options...
laffin Posted January 29, 2008 Share Posted January 29, 2008 array_count_values Quote Link to comment https://forums.phpfreaks.com/topic/88388-solved-count-distinct-values-in-array/#findComment-452371 Share on other sites More sharing options...
p2grace Posted January 29, 2008 Share Posted January 29, 2008 Wow that's a nice function, learn something new every day Quote Link to comment https://forums.phpfreaks.com/topic/88388-solved-count-distinct-values-in-array/#findComment-452375 Share on other sites More sharing options...
musclehead Posted January 29, 2008 Author Share Posted January 29, 2008 Perfect - absolutely perfect!! Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/88388-solved-count-distinct-values-in-array/#findComment-452393 Share on other sites More sharing options...
laffin Posted January 29, 2008 Share Posted January 29, 2008 Wow that's a nice function, learn something new every day that's what i say a lot here too, ya learn something from different ppl. Quote Link to comment https://forums.phpfreaks.com/topic/88388-solved-count-distinct-values-in-array/#findComment-452397 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.