1

e.g. "I think I got a long way to go to market."

In this case, I want to output

I 2
think 1
got 1
a 1
long 1
way 1
to 2
go 1
market 1

Code:

<?php
$tmp = explode (" ", $text);
    echo "
         
        My Words
        Repetition
        ";
        
         foreach ($tmp as $a)
        {
            
            echo "" $a."" ;
            echo ""  ;
        }       
        echo ""
?>
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • 1
    Does this answer your question? [PHP - count specific array values](https://stackoverflow.com/questions/11646054/php-count-specific-array-values) – nice_dev Oct 26 '22 at 08:38

1 Answers1

1

You can try with

$text = "I think I got a long way to go to market.";
$tmp = explode (" ", $text);
foreach($tmp as $a){
   $result[$a] += 1;
}
foreach($result as $value => $occurrences){
   echo "$value $occurrences\n";
}
Valkoff
  • 46
  • 5