3v4l.org

run code in 300+ PHP versions simultaneously
<?php function flatten($input_data, $path = "", $delimiter = ".", &$output = null) { if (is_null($output)) { $output = []; } if (is_array($input_data)) { if (isAssoc($input_data)) { array_push($output, [$path, new stdClass()]); foreach ($input_data as $key => $value) { if (!is_string($key)) { throw new Exception("Dictionary key must be a string, found " . gettype($key) . " instead."); } $new_key = $path ? $path . $delimiter . $key : $key; if (strpos($key, $delimiter) !== false) { throw new Exception("Key '{$key}' contains the delimiter '{$delimiter}'."); } flatten($value, $new_key, $delimiter, $output); } } else { array_push($output, [$path, []]); foreach ($input_data as $i => $value) { $new_key = $path ? $path . $delimiter . $i : strval($i); flatten($value, $new_key, $delimiter, $output); } } } else { array_push($output, [$path, $input_data]); } return $output; } function isAssoc(array $arr) { if (array() === $arr) return false; return array_keys($arr) !== range(0, count($arr) - 1); } function unflatten($array_data, $delimiter = '.') { $root = $array_data[0][1]; if (!is_array($root) && !is_object($root)) { return $root; } array_shift($array_data); foreach ($array_data as [$path, $value]) { $path_parts = explode($delimiter, $path); $target = &$root; foreach ($path_parts as $part) { if (is_numeric($part)) { $part = intval($part); } if (is_array($target)) { $target = &$target[$part]; } elseif (is_object($target)) { $target = &$target->$part; } else { // Handle error } } $target = $value; } return $root; } function test_flatten_unflatten() { $test_cases = [ [ 'input' => ['name' => 'John', 'age' => 30], 'expected_flatten' => [ ['', new stdClass()], ['name', 'John'], ['age', 30] ] ], [ 'input' => ['animals' => ['dog', 'cat']], 'expected_flatten' => [ ['', new stdClass()], ['animals', []], ['animals.0', 'dog'], ['animals.1', 'cat'] ] ], [ 'input' => 'hello', 'expected_flatten' => [ ['', 'hello'] ] ], [ 'input' => [], 'expected_flatten' => [ ['', []] ] ], [ 'input' => new stdClass(), 'expected_flatten' => [ ['', new stdClass()] ] ] ]; foreach ($test_cases as $i => $test_case) { $input = $test_case['input']; $expected_flatten = $test_case['expected_flatten']; $flattened = flatten($input); if ($flattened === $expected_flatten) { echo "Test case $i for flatten: Passed\n"; } else { echo "Test case $i for flatten: Failed\n"; print_r($flattened); } $unflattened = unflatten($flattened); if ($unflattened == $input) { echo "Test case $i for unflatten: Passed\n"; } else { echo "Test case $i for unflatten: Failed\n"; print_r($unflattened); } } } // Run the test test_flatten_unflatten(); ?>

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.00716.80
8.5.00.0140.00920.06
8.4.150.0030.00014.05
8.4.140.0120.00917.80
8.4.130.0100.01018.90
8.4.120.0150.00524.24
8.4.110.0120.00819.10
8.4.100.0050.00418.96
8.4.90.0120.00922.07
8.4.80.0060.00320.54
8.4.70.0120.01017.70
8.4.60.0130.00818.95
8.4.50.0040.00518.68
8.4.40.0030.00717.91
8.4.30.0100.01018.95
8.4.20.0170.00317.97
8.4.10.0000.00919.57
8.3.280.0160.00818.48
8.3.270.0090.01216.53
8.3.260.0100.00916.74
8.3.250.0050.00418.94
8.3.240.0090.01017.21
8.3.230.0050.00416.30
8.3.220.0100.00818.97
8.3.210.0070.00116.57
8.3.200.0040.00516.73
8.3.190.0110.00919.09
8.3.180.0110.00816.99
8.3.170.0090.00919.00
8.3.160.0060.00317.00
8.3.150.0090.00019.08
8.3.140.0040.00417.03
8.3.130.0060.00318.43
8.3.120.0030.00918.79
8.3.110.0060.00320.94
8.3.100.0060.00324.06
8.3.90.0110.00426.77
8.3.80.0120.00017.97
8.3.70.0120.00916.50
8.3.60.0090.00616.63
8.3.50.0040.01818.53
8.3.40.0100.00322.08
8.3.30.0070.01019.38
8.3.20.0040.00424.18
8.3.10.0040.00424.66
8.3.00.0040.00426.16
8.2.290.0060.01320.25
8.2.280.0040.00418.20
8.2.270.0040.00417.18
8.2.260.0040.00416.59
8.2.250.0040.00418.24
8.2.240.0040.00417.21
8.2.230.0000.00922.58
8.2.220.0040.00437.54
8.2.210.0050.00326.77
8.2.200.0030.00616.50
8.2.190.0130.00916.63
8.2.180.0180.00016.50
8.2.170.0000.01619.09
8.2.160.0160.00322.96
8.2.150.0000.00825.66
8.2.140.0050.00324.66
8.2.130.0040.00418.09
8.2.120.0040.00426.16
8.2.110.0060.00521.26
8.2.100.0060.00619.71
8.2.90.0110.00220.61
8.2.80.0020.00919.72
8.2.70.0070.00419.76
8.2.60.0080.00418.87
8.2.50.0080.00318.71
8.2.40.0040.00618.78
8.2.30.0060.00518.76
8.2.20.0070.00318.65
8.2.10.0080.00318.63
8.2.00.0090.00218.70
8.1.330.0160.00321.82
8.1.320.0090.00718.02
8.1.310.0120.01216.09
8.1.300.0120.00618.49
8.1.290.0080.00330.84
8.1.280.0040.01125.92
8.1.270.0040.00424.66
8.1.260.0070.00026.35
8.1.250.0000.00828.09
8.1.240.0040.00719.43
8.1.230.0050.00518.53
8.1.220.0030.00818.61
8.1.210.0080.00319.84
8.1.200.0050.00518.59
8.1.190.0050.00518.57
8.1.180.0040.00718.44
8.1.170.0050.00518.38
8.1.160.0070.00418.70
8.1.150.0070.00319.46
8.1.140.0070.00419.34
8.1.130.0090.00219.42
8.1.120.0000.01019.49
8.1.110.0100.00020.36
8.1.100.0070.00319.40
8.1.90.0070.00519.42
8.1.80.0090.00219.38
8.1.70.0070.00318.80
8.1.60.0050.00518.68
8.1.50.0070.00418.61
8.1.40.0050.00518.62
8.1.30.0070.00418.70
8.1.20.0100.00018.75
8.1.10.0060.00418.44
8.1.00.0060.00318.69
8.0.300.0090.00217.81
8.0.290.0080.00318.75
8.0.280.0050.00518.64
8.0.270.0050.00518.71
8.0.260.0070.00317.87
8.0.250.0050.00517.96
8.0.240.0060.00418.76
8.0.230.0060.00417.87
8.0.220.0080.00217.87
8.0.210.0050.00517.77
8.0.200.0020.00817.90
8.0.190.0080.00217.90
8.0.180.0070.00317.93
8.0.170.0030.00717.92
8.0.160.0050.00517.92
8.0.150.0070.00317.73
8.0.140.0050.00517.85
8.0.130.0070.00418.65
8.0.120.0050.00518.72
8.0.110.0080.00218.84
8.0.100.0070.00317.88
8.0.90.0090.00217.74
8.0.80.0050.00517.77
8.0.70.0070.00317.59
8.0.60.0020.00917.68
8.0.50.0050.00517.82
8.0.30.0080.00218.01
8.0.20.0100.00018.02
8.0.10.0070.00317.95

preferences:
34.24 ms | 403 KiB | 5 Q