3v4l.org

run code in 300+ PHP versions simultaneously
<?php function reverseRecursive($array, $recursing = false) { foreach ($array as $key => $value) { if (is_array($value)) { $array[$key] = '(' . reverseRecursive($value, true) . ')'; } } $first = $recursing ? array_shift($array) . ',' : ''; return $first . implode(',', array_reverse($array)); } $data = '(X,1,1,3),(X,1,1,3),(X,@2,@2,@2,@2,@2,(Y,1))'; $json = preg_replace(['/[^(),]+/', '/\(/', '/\)/'], ['"$0"', '[', ']'], "($data)"); $inverse = reverseRecursive(json_decode($json)); echo $inverse;

preferences:
29.65 ms | 408 KiB | 5 Q