<?php echo implode("<br>", lookAndSay(0, 5)); function lookAndSay(string $num, int $count): array { $results = [$num]; for ($i = 0; $i < $count; $i++) { preg_match_all('/(\d)\1*/', $num, $matches); $num = implode('', array_map(function ($item) { return strlen($item) . $item[0]; }, $matches[0])); $results[] = $num; } return $results; }