[PHP] Counting the occurences of items in an array
Has this wheel been invented, but simpler? I've an array of words that I'd like to know how many occurences of each there are. For instance: $fruits = array( lemon, orange, banana, apple, orange, banana, orange); And I'd like to create this: $fruit_count = array( lemon = 1, orange = 3, banana = 2, apple = 1); My current plan of action is to create $fruit_count, and check if $fruits[0] is listed in it. If not, then I'll add it with a value of 1. If it is already listed then I'll just increase it's number. Ditto for $fruits[1] and so on... Has someone cleverer than myself found a better way of doing this? This function will be used as a word count for text documents, and some of them have over 5000 words. So if there's a better way of doing this I'd love to know. Thanks. Dotan Cohen http://what-is-what.com/what_is/html_email.html http://lyricslist.com/lyrics/artist_albums/162/disturbed.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Counting the occurences of items in an array
Dotan Cohen wrote: Has this wheel been invented, but simpler? I've an array of words that I'd like to know how many occurences of each there are. For instance: $fruits = array( lemon, orange, banana, apple, orange, banana, orange); And I'd like to create this: $fruit_count = array( lemon = 1, orange = 3, banana = 2, apple = 1); check http://in2.php.net/manual/en/function.array-count-values.php -- Thanking You Sumeet Shroff http://www.prateeksha.com Web Designers and PHP / Mysql Ecommerce Development, Mumbai India -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Counting the occurences of items in an array
At 8:35 PM +0200 12/24/06, Dotan Cohen wrote: Has this wheel been invented, but simpler? I've an array of words that I'd like to know how many occurences of each there are. For instance: $fruits = array( lemon, orange, banana, apple, orange, banana, orange); And I'd like to create this: $fruit_count = array( lemon = 1, orange = 3, banana = 2, apple = 1); My current plan of action is to create $fruit_count, and check if $fruits[0] is listed in it. If not, then I'll add it with a value of 1. If it is already listed then I'll just increase it's number. Ditto for $fruits[1] and so on... Has someone cleverer than myself found a better way of doing this? This function will be used as a word count for text documents, and some of them have over 5000 words. So if there's a better way of doing this I'd love to know. Thanks. Dotan Cohen Dotan: array_count_values() tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Counting the occurences of items in an array
Thanks for the link to the function. Know that I did RTFM before posting, but I missed that one. Dotan Cohen http://lyricslist.com/lyrics/artist_albums/219/xyz.php http://simplesniff.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php