3v4l.org

run code in 300+ PHP versions simultaneously
<?php $input = '1_2_3_4_5'; // Get an array with 1, 2, 3, 4 and 5 $parts = explode('_', $input); $output = []; $i = 1; foreach ($parts as $part) { // array_slice() will take 1 to n elements from the array of numbers $values = array_slice($parts, 0, $i++); // Join the values $output[] = implode('_', $values); } print_r($output);

preferences:
46.33 ms | 402 KiB | 5 Q