Jump to content

[SOLVED] [need assistance] Count number of same value in string


andyhajime

Recommended Posts

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......  :shy:

 

Link to comment
Share on other sites

Thanks bro  :D.

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.  ;D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.