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