3v4l.org

run code in 300+ PHP versions simultaneously
<?php function join_words($words) { $return = []; for ($i=0; $i < count($words); $i++) { $return[] = implode(' ', array_slice($words, 0, $i+1)); } return $return; } $words = ['THIS', 'IS', 'AN', 'EXAMPLE']; $result = []; $result = array_merge($result, array_reverse(join_words($words))); $result = array_merge($result, array_reverse(join_words(array_reverse($words)))); print_r($result);

preferences:
42.14 ms | 1421 KiB | 5 Q