3v4l.org

run code in 300+ PHP versions simultaneously
<?php $text = "Rocket abc bla blub bla Rocket"; $textArr = explode(" ", $text); $wordCountArr = []; foreach ($textArr as $word) { if (!isset($wordCountArr[$word])) { $wordCountArr[$word] = 1; } else { $wordCountArr[$word]++; } } natcasesort($wordCountArr); print_r($wordCountArr); $textOrderArr = $wordCountArr; $inversedArr = []; foreach ($textOrderArr as $word => $count) { $inversedArr[$count][] = $word; } krsort($inversedArr); foreach ($inversedArr as $count => $words) { foreach ($words as $word) { echo $count . " x " . $word; echo PHP_EOL; } }

preferences:
42.06 ms | 402 KiB | 5 Q