3v4l.org

run code in 300+ PHP versions simultaneously
<?php $testArray = [ ['lev 1' => 'lev 1'], ['lev 2', 'lev 2 - 1', ['lev 2 - 2'], ['Key preserved' => 'lev 2 - 3']], [['lev 3 - 1', 'lev 3 - 2', 'lev 3 - 3']], [[[888 => 'lev 4']]] ]; // $element must initially be an array. Anything that is not an array will just return the accumulator. function flatten($element, $accum = []) { // If we don't have an array return the accumulator. if (!is_array($element)) { return $accum; } foreach($element as $key => $e) { // Check if the elements are an array or not. If not, append the accumulator with a key and value. if (!is_array($e)) { // Check that the key does not already exist, if it does we have to simply append to preserve values. if (array_key_exists($key, $accum)) { $accum[] = $e; } else { $accum[$key] = $e; } } // Only recurse when we have an array. if (is_array($e)) { // We pass in the accumulator and any values further down then hierachy. $vals = flatten($e, $accum); // We might get an empty array back if there was an array of arrays. if (!empty($vals)) { // We don't append our values here, we override the accumulator. The appending happens above when $e is not an // array. $accum = $vals; } } } // Return values of this array and all child arrays. return $accum; } var_dump(flatten($testArray));

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.4.20.0070.01319.27
8.4.10.0040.00419.50
8.3.150.0040.01516.61
8.3.140.0120.00617.25
8.3.130.0050.00518.54
8.3.120.0060.00318.93
8.3.110.0050.00520.94
8.3.100.0050.00524.06
8.3.90.0040.00426.77
8.3.80.0060.00316.63
8.3.70.0120.00316.58
8.3.60.0150.00316.50
8.3.50.0090.01124.36
8.3.40.0140.00321.90
8.3.30.0150.00018.84
8.3.20.0080.00024.18
8.3.10.0050.00324.66
8.3.00.0080.00026.16
8.2.260.0040.00418.39
8.2.250.0030.00616.63
8.2.240.0000.00820.98
8.2.230.0060.01322.58
8.2.220.0110.00037.54
8.2.210.0080.00026.77
8.2.200.0040.00418.18
8.2.190.0150.00716.88
8.2.180.0120.00325.92
8.2.170.0060.01218.91
8.2.160.0130.00722.96
8.2.150.0080.00025.66
8.2.140.0050.00324.66
8.2.130.0000.00726.16
8.2.120.0000.00720.02
8.2.110.0170.00719.39
8.2.100.0090.00317.72
8.2.90.0050.00317.75
8.2.80.0030.00617.97
8.2.70.0090.00017.63
8.2.60.0090.00017.75
8.2.50.0040.00418.04
8.2.40.0030.00619.24
8.2.30.0050.00219.43
8.2.20.0040.00418.09
8.2.10.0040.00419.29
8.2.00.0030.00519.19
8.1.310.0070.01018.54
8.1.300.0160.00317.82
8.1.290.0060.00330.84
8.1.280.0070.00725.92
8.1.270.0050.00224.66
8.1.260.0040.00426.35
8.1.250.0040.00428.09
8.1.240.0060.00921.85
8.1.230.0080.00322.70
8.1.220.0080.00017.79
8.1.210.0060.00318.77
8.1.200.0040.00717.35
8.1.190.0000.00817.35
8.1.180.0080.00018.10
8.1.170.0000.00817.62
8.1.160.0040.00418.95
8.1.150.0050.00718.89
8.1.140.0040.00418.89
8.1.130.0050.00217.45
8.1.120.0000.00817.47
8.1.110.0060.00317.35
8.1.100.0000.00717.50
8.1.90.0040.00417.36
8.1.80.0030.00717.38
8.1.70.0000.00717.46
8.1.60.0030.00617.65
8.1.50.0000.00717.57
8.1.40.0040.00417.56
8.1.30.0000.00817.66
8.1.20.0060.00317.63
8.1.10.0030.00617.42
8.1.00.0090.00017.43
8.0.300.0040.00420.03
8.0.290.0000.00716.75
8.0.280.0000.00718.45
8.0.270.0060.00316.88
8.0.260.0030.00316.74
8.0.250.0070.00016.86
8.0.240.0070.00017.00
8.0.230.0000.00716.98
8.0.220.0000.00716.84
8.0.210.0000.00816.86
8.0.200.0030.00316.85
8.0.190.0040.00416.95
8.0.180.0070.00016.93
8.0.170.0060.00316.80
8.0.160.0050.00216.90
8.0.150.0040.00416.76
8.0.140.0000.00816.74
8.0.130.0030.00313.41
8.0.120.0040.00416.83
8.0.110.0040.00416.69
8.0.100.0040.00416.90
8.0.90.0040.00416.96
8.0.80.0100.00716.86
8.0.70.0030.00516.79
8.0.60.0030.00316.76
8.0.50.0000.00716.92
8.0.30.0090.00816.99
8.0.20.0070.01117.03
8.0.10.0060.00917.00
8.0.00.0120.00916.87
7.4.330.0030.00315.55
7.4.320.0000.00716.59
7.4.300.0000.00816.52
7.4.290.0080.00016.52
7.4.280.0000.00716.50
7.4.270.0030.00316.57
7.4.260.0000.00513.32
7.4.250.0040.00416.57
7.4.240.0040.00416.52
7.4.230.0000.00716.45
7.4.220.0030.00316.60
7.4.210.0100.00716.45
7.4.200.0030.00616.58
7.4.160.0080.01116.59
7.4.150.0070.00816.56
7.4.140.0150.00016.65
7.4.130.0110.01016.40
7.4.120.0100.01216.41
7.4.110.0080.00916.49
7.4.100.0090.01016.47
7.4.90.0020.01516.36
7.4.80.0120.00816.59
7.4.70.0110.00616.45
7.4.60.0040.01416.56
7.4.50.0120.00916.38
7.4.40.0110.01116.44
7.4.30.0100.00916.40
7.4.20.0090.00816.31
7.4.10.0090.00716.53
7.4.00.0120.00316.55
7.3.330.0030.00316.35
7.3.320.0030.00313.18
7.3.310.0000.00716.26
7.3.300.0070.00016.42
7.3.290.0060.01016.33
7.3.270.0070.00716.38
7.3.260.0140.00716.53
7.3.250.0070.01216.43
7.3.240.0070.01216.42
7.3.230.0110.00616.35
7.3.220.0080.01116.33
7.3.210.0170.00116.43
7.3.200.0080.01116.41
7.3.190.0100.00716.42
7.3.180.0070.01016.46
7.3.170.0100.00816.36
7.3.160.0090.00716.39
7.3.150.0050.01416.50
7.3.140.0090.00816.39
7.3.130.0090.00916.40
7.3.120.0120.00516.38
7.3.110.0050.01116.46
7.3.100.0120.00816.44
7.3.90.0100.01116.45
7.3.80.0090.01416.38
7.3.70.0110.00816.43
7.3.60.0080.00816.39
7.3.50.0050.01216.45
7.3.40.0090.00816.41
7.3.30.0110.00616.49
7.3.20.0110.00516.51
7.3.10.0150.00416.40
7.3.00.0450.00916.41

preferences:
79.21 ms | 403 KiB | 5 Q