jjk2 Posted May 2, 2009 Share Posted May 2, 2009 what if i want to compare between multiple strings and show the count of the number of times a keyword repeats? for ex) apple blue green apple yellow not apple something blue apple drugs car yellow apple count is 4, yellow count is 2, blue count is 2. Quote Link to comment https://forums.phpfreaks.com/topic/156494-what-if-i-want-to-compare-multiple-strings-and-count-the-number-of-times-a-k/ Share on other sites More sharing options...
ignace Posted May 2, 2009 Share Posted May 2, 2009 To easy: <?php $strings = array( 'apple blue green', 'apple yellow not', 'apple something blue', 'apple drugs car yellow' ); $wordcounts = array(); foreach ($strings as $string) { $words = explode(' ', $string); foreach ($words as $word) { if (isset($wordcounts[$word])) { ++$wordcounts[$word]; } else { $wordcounts[$word] = 1; } } } print_r($wordcounts); ?> Quote Link to comment https://forums.phpfreaks.com/topic/156494-what-if-i-want-to-compare-multiple-strings-and-count-the-number-of-times-a-k/#findComment-824091 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.