How can one count the occurrences of each word in a comma-separated string in PHP?

To count the occurrences of each word in a comma-separated string in PHP, you can first use the explode() function to split the string into an array of words. Then, you can use the array_count_values() function to count the occurrences of each word in the array. Finally, you can loop through the resulting array to display the word and its count.

$string = "apple,banana,apple,orange,banana,apple";
$words = explode(",", $string);
$wordCount = array_count_values($words);

foreach($wordCount as $word => $count) {
    echo $word . ": " . $count . "<br>";
}