3v4l.org

run code in 300+ PHP versions simultaneously
<?php function flatten($array, $prefix = '') { $return = []; foreach ($array as $key => $value) { if (is_array($value)) { $return = array_merge($return, flatten($value, $prefix . $key . '_')); } else { $return[$prefix . $key] = $value; } } return $return; } // proof that it's working on the simple cases var_dump(flatten([ 1, 2, 3 => [4, 5] ])); var_dump(flatten([ [1, 2], [3, 4] ])); // use array_values on the result if you don't care about keys // simple multidimensional array var_dump(flatten([ 'a' => [ 'b' => 'value', ] ])); // proof that it's not overwriting values like all the other solutions var_dump(flatten([ 'a' => [ 'b' => 'value', ], 'c' => [ 'b' => 'anothervalue', ], ]));

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.3.130.0050.00318.21
8.3.120.0050.00320.67
8.3.110.0110.00316.50
8.3.100.0050.00524.06
8.3.90.0090.00626.77
8.3.80.0090.00016.63
8.3.70.0160.00316.58
8.3.60.0110.00718.31
8.3.50.0140.00524.44
8.3.40.0180.00418.67
8.3.30.0070.01118.67
8.3.20.0040.00824.18
8.3.10.0070.00024.66
8.3.00.0050.00326.16
8.2.250.0040.00416.84
8.2.240.0120.00618.93
8.2.230.0040.00420.94
8.2.220.0090.00037.54
8.2.210.0090.00926.77
8.2.200.0060.00316.59
8.2.190.0120.00316.63
8.2.180.0070.01425.92
8.2.170.0120.00319.09
8.2.160.0120.00322.96
8.2.150.0000.00825.66
8.2.140.0040.00424.66
8.2.130.0040.00426.16
8.2.120.0000.00726.35
8.2.110.0030.00621.03
8.2.100.0070.00317.78
8.2.90.0060.00320.47
8.2.80.0050.00518.89
8.2.70.0000.00818.04
8.2.60.0040.00417.75
8.2.50.0050.00318.10
8.2.40.0030.00619.29
8.2.30.0000.00819.25
8.2.20.0080.00018.18
8.2.10.0070.00019.30
8.2.00.0070.00019.32
8.1.300.0030.00617.76
8.1.290.0060.00330.84
8.1.280.0130.00725.92
8.1.270.0070.00024.66
8.1.260.0040.00428.09
8.1.250.0040.00428.09
8.1.240.0000.00919.14
8.1.230.0000.01120.91
8.1.220.0030.00617.74
8.1.210.0040.00418.95
8.1.200.0000.00817.23
8.1.190.0040.00417.35
8.1.180.0030.00518.10
8.1.170.0000.00818.58
8.1.160.0030.00619.03
8.1.150.0050.00318.77
8.1.140.0030.00618.95
8.1.130.0070.00017.46
8.1.120.0000.00817.40
8.1.110.0040.00417.35
8.1.100.0000.00717.38
8.1.90.0050.00217.46
8.1.80.0080.00017.45
8.1.70.0040.00417.43
8.1.60.0040.00617.55
8.1.50.0090.00217.40
8.1.40.0090.00317.45
8.1.30.0100.00217.63
8.1.20.0100.00517.61
8.1.10.0100.00517.50
8.1.00.0130.00317.39
8.0.300.0080.00020.08
8.0.290.0070.00316.75
8.0.280.0000.00718.45
8.0.270.0040.00416.82
8.0.260.0030.00316.93
8.0.250.0000.00716.87
8.0.240.0070.00016.82
8.0.230.0030.00316.85
8.0.220.0040.00416.89
8.0.210.0030.00316.90
8.0.200.0020.00516.99
8.0.190.0040.00616.89
8.0.180.0040.00816.90
8.0.170.0100.00316.86
8.0.160.0080.00516.90
8.0.150.0080.00516.72
8.0.140.0100.00616.84
8.0.130.0090.00515.63
8.0.120.0060.00816.87
8.0.110.0080.00516.90
8.0.100.0100.00516.85
8.0.90.0070.00816.78
8.0.80.0090.00716.83
8.0.70.0120.00316.77
8.0.60.0080.00516.78
8.0.50.0050.00616.81
8.0.30.0100.00516.88
8.0.20.0080.00917.01
8.0.10.0080.00316.98
8.0.00.0080.00616.96
7.4.330.0050.00015.55
7.4.320.0030.00316.50
7.4.300.0020.00616.54
7.4.290.0060.00316.49
7.4.280.0100.00216.59
7.4.270.0080.00216.56
7.4.260.0100.00415.38
7.4.250.0100.00416.51
7.4.240.0100.00416.54
7.4.230.0080.00316.45
7.4.220.0050.00516.50
7.4.210.0120.00416.50
7.4.200.0120.00116.46
7.4.190.0110.00516.54
7.4.180.0090.00516.51
7.4.160.0080.00616.60
7.4.150.0110.00416.43
7.4.140.0100.00516.38
7.4.130.0090.01116.37
7.4.120.0100.00716.47
7.4.110.0080.00816.45
7.4.100.0100.00716.54
7.4.90.0100.00516.45
7.4.80.0100.00716.53
7.4.70.0130.00516.40
7.4.60.0130.00716.49
7.4.50.0100.00616.41
7.4.40.0020.01016.35
7.4.30.0100.00816.42
7.4.20.0110.00616.43
7.4.10.0100.00616.47
7.4.00.0130.00316.44
7.3.330.0110.00216.30
7.3.320.0070.00615.31
7.3.310.0100.00316.31
7.3.300.0120.00316.26
7.3.290.0110.00416.32
7.3.280.0150.00316.27
7.3.270.0130.00416.40
7.3.260.0090.00916.45
7.3.250.0110.00616.27
7.3.240.0110.00716.39
7.3.230.0120.00516.36
7.3.220.0110.00616.25
7.3.210.0110.00616.39
7.3.200.0090.01016.36
7.3.190.0150.00316.20
7.3.180.0140.00416.29
7.3.170.0100.00816.34
7.3.160.0100.00816.31
7.3.150.0120.00516.30
7.3.140.0100.01016.29
7.3.130.0130.00616.23
7.3.120.0110.00516.29
7.3.110.0090.00916.15
7.3.100.0110.00616.29
7.3.90.0130.00416.42
7.3.80.0120.00516.41
7.3.70.0130.00416.39
7.3.60.0110.00816.45
7.3.50.0140.00216.53
7.3.40.0140.00316.49
7.3.30.0140.00516.45
7.3.20.0110.00817.62
7.3.10.0150.00317.49
7.3.00.0080.01017.59
7.2.340.0100.00616.37
7.2.330.0120.00616.44
7.2.320.0110.00916.38
7.2.310.0110.00716.39
7.2.300.0120.00616.46
7.2.290.0120.00716.49
7.2.280.0100.00716.48
7.2.270.0100.00716.39
7.2.260.0130.00516.37
7.2.250.0110.00616.37
7.2.240.0150.00316.41
7.2.230.0120.00516.52
7.2.220.0130.00516.44
7.2.210.0100.00816.43
7.2.200.0140.00716.56
7.2.190.0140.00316.58
7.2.180.0100.00616.55
7.2.170.0100.00716.65
7.2.160.0110.00716.55
7.2.150.0130.00717.89
7.2.140.0140.00417.86
7.2.130.0130.00417.98
7.2.120.0090.01117.81
7.2.110.0160.00317.92
7.2.100.0120.00617.88
7.2.90.0240.00817.90
7.2.80.0140.00517.76
7.2.70.0100.01017.80
7.2.60.0120.00617.78
7.2.50.0130.00517.94
7.2.40.0130.00517.90
7.2.30.0140.00417.87
7.2.20.0090.00817.92
7.2.10.0150.00217.86
7.2.00.0110.00717.79

preferences:
31.32 ms | 403 KiB | 5 Q