andyhajime Posted September 24, 2009 Share Posted September 24, 2009 Hey guys, I need some assistance in how to do this. Been drowning myself with coffee the whole night with no success. I've read the str_word_count, explode, count_chars and it still doesn't ring any bell. Hope you guys, the experts, can shed some light into this. I have this string: $code = "big, spiders, funny, scary, big, ugly, giant, mother, big, spiders, babies, hatch, eggs, big, creepy, big, spiders, garage, funny, sequel"; If you look at the value, you notice that there's 5 big, 3 spiders, 2 funny, while the rest is only 1 of each. I've been trying to echo out the quantity of each word inside $code like this: "big" has 5 "spider" has 3 "funny" has 2 "scary" has 1 "ugly" has 1 "giant" has 1 ect.... ...and not repeating the same word which already stated earlier like this. "big" has 5 "spider" has 3 "funny" has 2 "scary" has 1 "big" has 5 <--- REPEATED "ugly" has 1 "giant" has 1 ect.... Appreciate the help guys...... Link to comment https://forums.phpfreaks.com/topic/175317-solved-need-assistance-count-number-of-same-value-in-string/ Share on other sites More sharing options...
Garethp Posted September 24, 2009 Share Posted September 24, 2009 Well, I would do this $Str = split($code, ","); $New = array(); foreach($Str as $v) { $v = str_replace(' ', '', $v) if(isset($New[$v])) { $New[$v] ++; } else { $New[$v] = 1; } } foreach($New as $k=>$v) { echo "\"$k\" has $v<br>"; } Link to comment https://forums.phpfreaks.com/topic/175317-solved-need-assistance-count-number-of-same-value-in-string/#findComment-923946 Share on other sites More sharing options...
andyhajime Posted September 24, 2009 Author Share Posted September 24, 2009 Thanks bro . I did test it out and it echoed this: "," has 1 I made some adjustment and trimming which is now fully workable, though not sure if it's advisable. Here's the codes for share if anyone has the same issue as me. $Str = explode(", ",$code); $New = array(); foreach($Str as $v) { $New[$v] ++; } foreach($New as $k=>$v) { echo "\"$k\" has $v<br>"; } ...and again, thanks Garethp. If you're in Singapore right now, I would definitely give you a treat. I finally can head off to bed but can't sleep. Drowsy with caffeine. Link to comment https://forums.phpfreaks.com/topic/175317-solved-need-assistance-count-number-of-same-value-in-string/#findComment-923951 Share on other sites More sharing options...
Garethp Posted September 24, 2009 Share Posted September 24, 2009 Hahah, no such luck. I only lived there for a couple years in 2000 when my dad worked there Link to comment https://forums.phpfreaks.com/topic/175317-solved-need-assistance-count-number-of-same-value-in-string/#findComment-923952 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.