3v4l.org

run code in 500+ 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.5.90.0080.00617.77
8.5.80.0090.00517.56
8.5.70.0100.00717.79
8.5.60.0130.00617.71
8.5.50.0110.00717.47
8.5.40.0090.01018.61
8.5.30.0130.00717.85
8.5.20.0130.00717.50
8.5.10.0150.00516.69
8.5.00.0140.00818.36
8.4.240.0160.00719.56
8.4.230.0080.00919.61
8.4.220.0140.00919.64
8.4.210.0170.00719.50
8.4.200.0250.00819.62
8.4.190.0160.00819.75
8.4.180.0150.00819.76
8.4.170.0120.01121.85
8.4.160.0130.01221.91
8.4.150.0120.00517.91
8.4.140.0120.01117.76
8.4.130.0150.00718.29
8.4.120.0160.00621.18
8.4.110.0120.01118.43
8.4.100.0170.00718.41
8.4.90.0140.00819.29
8.4.80.0130.00819.31
8.4.70.0130.01018.31
8.4.60.0140.01018.31
8.4.50.0140.00619.07
8.4.40.0150.00617.74
8.4.30.0140.00919.88
8.4.20.0140.00918.31
8.4.10.0120.00920.07
8.3.330.0170.00718.50
8.3.320.0120.00818.49
8.3.310.0110.01018.48
8.3.300.0140.00819.57
8.3.290.0130.01019.61
8.3.280.0150.00718.31
8.3.270.0130.00816.49
8.3.260.0130.00816.53
8.3.250.0150.00617.74
8.3.240.0130.00816.86
8.3.230.0140.00816.70
8.3.220.0120.00817.83
8.3.210.0120.00717.81
8.3.200.0120.00816.69
8.3.190.0120.01116.92
8.3.180.0150.00616.45
8.3.170.0130.00816.96
8.3.160.0100.00516.69
8.3.150.0130.00716.90
8.3.140.0080.01116.54
8.3.130.0120.00217.40
8.3.120.0100.00618.65
8.3.110.0140.00516.50
8.3.100.0100.00620.36
8.3.90.0120.00721.75
8.3.80.0120.00416.64
8.3.70.0170.00516.66
8.3.60.0110.00917.51
8.3.50.0140.00621.82
8.3.40.0190.00418.35
8.3.30.0100.01118.23
8.3.20.0160.00721.11
8.3.10.0130.00621.29
8.3.00.0110.00422.01
8.2.330.0180.00518.09
8.2.320.0150.00717.99
8.2.310.0110.01018.08
8.2.300.0240.00618.07
8.2.290.0180.00918.70
8.2.280.0170.00717.48
8.2.270.0150.01116.98
8.2.260.0170.00818.73
8.2.250.0160.00516.70
8.2.240.0200.00717.73
8.2.230.0160.00718.70
8.2.220.0180.00627.05
8.2.210.0190.00821.63
8.2.200.0180.00516.64
8.2.190.0190.00516.55
8.2.180.0190.01021.20
8.2.170.0220.00518.61
8.2.160.0200.00520.50
8.2.150.0160.00621.80
8.2.140.0150.00721.25
8.2.130.0160.00621.92
8.2.120.0180.00722.03
8.2.110.0160.00719.47
8.2.100.0140.00817.89
8.2.90.0140.00619.25
8.2.80.0160.00818.25
8.2.70.0140.00817.88
8.2.60.0100.00517.86
8.2.50.0130.00818.00
8.2.40.0140.00718.64
8.2.30.0120.00818.50
8.2.20.0180.00518.04
8.2.10.0180.00418.61
8.2.00.0180.00418.58
8.1.340.0260.00817.62
8.1.330.0200.00718.98
8.1.320.0170.01017.07
8.1.310.0200.00817.28
8.1.300.0160.00816.84
8.1.290.0170.00423.35
8.1.280.0150.00821.02
8.1.270.0180.00521.10
8.1.260.0140.00522.87
8.1.250.0090.00622.92
8.1.240.0090.00918.46
8.1.230.0100.01019.29
8.1.220.0090.00817.77
8.1.210.0100.00618.34
8.1.200.0080.00917.38
8.1.190.0150.00517.35
8.1.180.0120.00617.77
8.1.170.0080.01118.06
8.1.160.0050.00519.24
8.1.150.0100.00618.08
8.1.140.0100.00618.24
8.1.130.0100.00417.51
8.1.120.0070.00717.56
8.1.110.0070.00317.52
8.1.100.0030.00717.53
8.1.90.0060.00417.46
8.1.80.0140.00317.57
8.1.70.0080.00517.48
8.1.60.0070.00817.60
8.1.50.0120.00417.50
8.1.40.0110.00517.54
8.1.30.0120.00417.70
8.1.20.0130.00517.68
8.1.10.0130.00617.60
8.1.00.0140.00417.48
8.0.300.0090.00418.56
8.0.290.0130.00516.90
8.0.280.0130.00517.70
8.0.270.0060.00316.93
8.0.260.0070.00516.83
8.0.250.0090.00716.93
8.0.240.0070.00216.99
8.0.230.0070.00216.98
8.0.220.0060.00616.97
8.0.210.0140.00316.81
8.0.200.0110.00616.97
8.0.190.0120.00616.96
8.0.180.0100.00716.92
8.0.170.0150.00316.97
8.0.160.0140.00616.95
8.0.150.0110.00616.78
8.0.140.0120.00716.87
8.0.130.0130.00615.94
8.0.120.0090.00816.91
8.0.110.0110.00616.89
8.0.100.0120.00516.84
8.0.90.0100.00816.82
8.0.80.0120.00716.81
8.0.70.0140.00316.77
8.0.60.0100.00616.81
8.0.50.0080.00616.84
8.0.30.0120.00616.93
8.0.20.0100.00917.02
8.0.10.0110.00417.04
8.0.00.0110.00616.98
7.4.330.0080.00516.23
7.4.320.0100.00317.51
7.4.300.0070.00616.57
7.4.290.0080.00516.97
7.4.280.0100.00417.09
7.4.270.0090.00417.06
7.4.260.0100.00616.15
7.4.250.0110.00517.01
7.4.240.0100.00516.94
7.4.230.0100.00416.90
7.4.220.0090.00516.51
7.4.210.0120.00516.83
7.4.200.0120.00216.99
7.4.190.0110.00517.19
7.4.180.0090.00617.19
7.4.160.0100.00717.24
7.4.150.0130.00417.12
7.4.140.0100.00616.47
7.4.130.0110.01016.41
7.4.120.0130.00616.51
7.4.110.0110.00816.48
7.4.100.0120.00716.57
7.4.90.0120.00616.49
7.4.80.0100.00816.53
7.4.70.0130.00516.40
7.4.60.0140.00716.95
7.4.50.0120.00616.86
7.4.40.0060.00816.85
7.4.30.0110.00816.91
7.4.20.0120.00616.91
7.4.10.0130.00716.51
7.4.00.0120.00416.48
7.3.330.0120.00316.40
7.3.320.0070.00716.08
7.3.310.0090.00316.82
7.3.300.0100.00316.80
7.3.290.0110.00416.35
7.3.280.0130.00316.78
7.3.270.0100.00417.07
7.3.260.0100.00816.84
7.3.250.0120.00616.39
7.3.240.0110.00616.67
7.3.230.0120.00616.85
7.3.220.0110.00616.95
7.3.210.0110.00616.86
7.3.200.0100.00916.82
7.3.190.0130.00416.72
7.3.180.0130.00516.78
7.3.170.0100.00816.84
7.3.160.0110.00616.82
7.3.150.0140.00616.38
7.3.140.0100.00916.80
7.3.130.0120.00616.76
7.3.120.0110.00616.77
7.3.110.0090.00916.66
7.3.100.0110.00616.74
7.3.90.0130.00516.97
7.3.80.0120.00616.90
7.3.70.0120.00516.87
7.3.60.0120.00716.97
7.3.50.0140.00417.04
7.3.40.0150.00317.00
7.3.30.0140.00616.89
7.3.20.0120.00817.80
7.3.10.0150.00417.67
7.3.00.0100.00917.73
7.2.340.0100.00717.02
7.2.330.0140.00616.48
7.2.320.0130.00716.42
7.2.310.0160.00616.43
7.2.300.0150.00716.50
7.2.290.0150.00716.51
7.2.280.0140.00716.49
7.2.270.0140.00716.44
7.2.260.0160.00516.37
7.2.250.0150.00716.37
7.2.240.0190.00316.48
7.2.230.0150.00616.54
7.2.220.0140.00616.48
7.2.210.0140.00816.48
7.2.200.0170.00716.61
7.2.190.0170.00416.57
7.2.180.0140.00616.55
7.2.170.0140.00716.69
7.2.160.0140.00716.60
7.2.150.0160.00718.06
7.2.140.0170.00418.05
7.2.130.0170.00418.14
7.2.120.0120.01018.04
7.2.110.0170.00518.07
7.2.100.0140.00818.05
7.2.90.0240.00818.10
7.2.80.0180.00617.97
7.2.70.0150.00917.99
7.2.60.0150.00617.96
7.2.50.0170.00518.13
7.2.40.0130.00618.10
7.2.30.0150.00418.07
7.2.20.0110.00818.08
7.2.10.0160.00318.09
7.2.00.0130.00717.97
5.6.400.0130.01115.71

preferences:
51.85 ms | 1686 KiB | 5 Q