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. 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); 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 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 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! 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 Quote 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. 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
Archived
This topic is now archived and is closed to further replies.