3v4l.org

run code in 300+ PHP versions simultaneously
<?php $lines = [ 'Company 1', 'Software Engineer', 'July 2020 - Present (1 month)', 'Pretoria, Gauteng, South Africa', 'Company 2', 'CTO', 'September 2016 - Present (3 years 11 months)', 'Pretoria, South Africa' ]; function computeExperiences(array $lines): array { $experiences = []; $position = 0; while ($chunkLines = array_slice($lines, $position, 4)) { $experience = array_slice($chunkLines, 0, 3); $locationIsPresent = isset($chunkLines[3]) && preg_match('/\w+,\s\w+(?:,\s\w+)?/', $chunkLines[3]); if ($locationIsPresent) { $experience[] = $chunkLines[3]; $position += 4; } else { $position += 3; } $experiences[] = $experience; } return $experiences; } print_r($lines); print_r(computeExperiences($lines));
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.4, 8.3.6 - 8.3.7
Array ( [0] => Company 1 [1] => Software Engineer [2] => July 2020 - Present (1 month) [3] => Pretoria, Gauteng, South Africa [4] => Company 2 [5] => CTO [6] => September 2016 - Present (3 years 11 months) [7] => Pretoria, South Africa ) Array ( [0] => Array ( [0] => Company 1 [1] => Software Engineer [2] => July 2020 - Present (1 month) [3] => Pretoria, Gauteng, South Africa ) [1] => Array ( [0] => Company 2 [1] => CTO [2] => September 2016 - Present (3 years 11 months) [3] => Pretoria, South Africa ) )
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Array ( [0] => Company 1 [1] => Software Engineer [2] => July 2020 - Present (1 month) [3] => Pretoria, Gauteng, South Africa [4] => Company 2 [5] => CTO [6] => September 2016 - Present (3 years 11 months) [7] => Pretoria, South Africa ) Array ( [0] => Array ( [0] => Company 1 [1] => Software Engineer [2] => July 2020 - Present (1 month) [3] => Pretoria, Gauteng, South Africa ) [1] => Array ( [0] => Company 2 [1] => CTO [2] => September 2016 - Present (3 years 11 months) [3] => Pretoria, South Africa ) )

preferences:
135.97 ms | 403 KiB | 183 Q