3v4l.org

run code in 300+ PHP versions simultaneously
<?php $person = [ 'id' => '123', 'name' => 'John Doe', 'relationship_status' => 'Married', 'work_profession' => 'Teacher', 'work_salary' => '1500', 'work_status' => 'Working', 'work_place' => 'Lorem City School', 'from_province' => 'Dolor Sit Province', 'from_city_name' => 'Ipsum City', 'from_city_address' => 'Some Rd. 1337' ]; function reShapeArray(array $arr): array { $return = []; foreach ($arr as $key => $value) { $firstUnderscore = strpos($key, '_'); if ($firstUnderscore === false) { // already at the lowest level, simply return as is $return[$key] = $value; } else { // there's an underscore, dig into this [$top, $sub] = explode('_', $key, 2); $return[$top] = array_merge_recursive($return[$top] ?? [], reShapeArray([$sub => $value])); } } return $return; } function mergeSingleElementArrays(array $arr): array { $return = []; foreach ($arr as $key => $value) { if (is_array($value) && count($value) === 1) { $subKey = array_key_first($value); $return["{$key}_{$subKey}"] = $value[$subKey]; } else { $return[$key] = $value; } } return $return; } $tests = [ 'deep array with single prefixed key' => [ ['a_b_c_d' => 'foo', 'a_b_e' => 'bar',], ['a_b' => ['c' => ['d' => 'foo'], 'e' => 'bar']], ], 'single prefixed key' => [['a_b' => 'foo'], ['a_b' => 'foo']], 'standard' => [ $person, [ 'id' => '123', 'name' => 'John Doe', 'relationship_status' => 'Married', 'work' => [ 'profession' => 'Teacher', 'salary' => '1500', 'status' => 'Working', 'place' => 'Lorem City School', ], 'from' => [ 'province' => 'Dolor Sit Province', 'city' => [ 'name' => 'Ipsum City', 'address' => 'Some Rd. 1337', ], ], ], ], ]; $errors = []; foreach ($tests as $name => [$input, $expected]) { $result = reShapeArray($input); $foo = mergeSingleElementArrays($result); if ($foo !== $expected) { $errors[] = "Test '$name' failed: got " . json_encode($foo) . ' vs expected ' . json_encode($expected); } } if ($errors) { var_dump($errors); } else { echo "All tests passed\n"; }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.5.10.0110.00916.65
8.5.00.0150.00620.16
8.4.150.0010.00114.05
8.4.140.0130.00917.68
8.4.130.0100.01018.66
8.4.120.0060.00424.19
8.4.110.0120.00818.88
8.4.100.0140.00718.79
8.4.90.0120.00520.72
8.4.80.0120.00819.02
8.4.70.0100.00617.86
8.4.60.0110.01117.55
8.4.50.0050.00417.89
8.4.40.0100.01017.86
8.4.30.0030.01018.77
8.4.20.0120.00317.75
8.4.10.0190.00019.42
8.3.280.0120.00918.36
8.3.270.0110.00916.52
8.3.260.0100.00916.57
8.3.250.0120.00619.11
8.3.240.0100.00917.04
8.3.230.0080.00516.82
8.3.220.0090.00918.84
8.3.210.0090.01016.88
8.3.200.0130.00716.50
8.3.190.0090.01016.65
8.3.180.0120.00618.36
8.3.170.0060.00920.75
8.3.160.0100.01017.26
8.3.150.0080.00018.99
8.3.140.0160.00316.48
8.3.130.0080.00016.72
8.3.120.0030.00620.74
8.3.110.0060.00316.39
8.3.100.0030.00616.77
8.3.90.0090.00026.77
8.3.80.0080.00016.50
8.3.70.0160.00016.58
8.3.60.0000.01518.59
8.3.50.0040.01116.52
8.3.40.0100.00721.75
8.3.30.0040.01122.14
8.3.20.0030.00525.66
8.3.10.0040.00425.66
8.2.290.0120.00420.34
8.2.280.0100.00918.37
8.2.270.0040.00420.25
8.2.260.0040.00417.26
8.2.250.0080.00018.36
8.2.240.0080.00816.50
8.2.230.0040.00420.94
8.2.220.0110.00724.06
8.2.210.0080.00326.77
8.2.200.0080.00418.16
8.2.190.0140.00018.29
8.2.180.0120.00925.92
8.2.170.0040.01122.96
8.2.160.0040.01222.96
8.2.150.0120.00725.66
8.2.140.0100.00725.66
8.1.330.0110.00821.98
8.1.320.0090.00616.39
8.1.310.0000.00816.75
8.1.300.0150.00416.43
8.1.290.0030.00630.84
8.1.280.0060.00925.92
8.1.270.0000.00725.66

preferences:
68.41 ms | 403 KiB | 5 Q