3v4l.org

run code in 300+ PHP versions simultaneously
<?php function buildTreeHelper(&$array, $currentLevel = 1) { $result = array(); $lastIndex = 0; while($pair = each($array)) { list(, $row) = $pair; $level = $row['level']; if ($level > $currentLevel) { $result[$lastIndex]['children'] = buildTreeHelper($array, $level); } else if ($level == $currentLevel) { $result[++$lastIndex] = $row; } else { prev($array); // shift back break; } } return $result; } function buildTree($array) { reset($array); return buildTreeHelper($array); } $array = [ ['level'=>1, 'name' => 'Root #1'], ['level'=>1, 'name' => 'Root #2'], ['level'=>2, 'name' => 'subroot 2-1'], ['level'=>3, 'name' => '__subroot 2-1/1'], ['level'=>3, 'name' => '__subroot 2-1/2'], ['level'=>4, 'name' => '__subroot 2-2/1'], ['level'=>2, 'name' => 'subroot 2-2'], ['level'=>2, 'name' => 'subroot 2-3'], ['level'=>1, 'name' => 'Root #3'] ]; $time = microtime(true); var_dump(buildTree($array)); echo round(microtime(true) - $time, 10);

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)
7.4.00.0160.00315.17
7.3.120.0070.00714.91
7.3.110.0160.00314.83
7.3.100.0130.00715.04
7.3.90.0130.00314.73
7.3.80.0050.00514.95
7.3.70.0080.00815.04
7.3.60.0120.00314.84
7.3.50.0000.01214.81
7.3.40.0040.00714.72
7.3.30.0040.00714.95
7.3.20.0140.00016.73
7.3.10.0040.01116.66
7.3.00.0100.00416.67
7.2.250.0110.00315.33
7.2.240.0040.01214.91
7.2.230.0060.01015.09
7.2.220.0060.00614.90
7.2.210.0140.00015.08
7.2.200.0000.00915.21
7.2.190.0030.01315.11
7.2.180.0070.00314.85
7.2.170.0000.01415.03
7.2.160.0000.01415.02
7.2.150.0070.00716.98
7.2.140.0080.00416.66
7.2.130.0050.00716.88
7.2.120.0090.00616.86
7.2.110.0050.00616.99
7.2.100.0090.00616.84
7.2.90.0030.01216.99
7.2.80.0020.00917.01
7.2.70.0060.00816.98
7.2.60.0070.00716.70
7.2.50.0030.01016.86
7.2.40.0030.01016.87
7.2.30.0020.01316.99
7.2.20.0070.00817.11
7.2.10.0050.00917.00
7.2.00.0050.01117.57
7.1.330.0150.00315.77
7.1.320.0110.00416.00
7.1.310.0030.01015.80
7.1.300.0040.00415.80
7.1.290.0000.01815.86
7.1.280.0000.00815.78
7.1.270.0090.00915.80
7.1.260.0100.00715.99
7.1.250.0100.00315.93
7.1.240.0060.00615.87
7.1.230.0150.00015.98
7.1.220.0090.00915.79
7.1.210.0070.01015.98
7.1.200.0030.00815.71
7.1.190.0070.01015.96
7.1.180.0110.00415.91
7.1.170.0120.00315.94
7.1.160.0030.00915.72
7.1.150.0070.01015.98
7.1.140.0090.00615.86
7.1.130.0080.00615.95
7.1.120.0000.01415.95
7.1.110.0070.00715.62
7.1.100.0030.01015.59
7.1.90.0060.01215.61
7.1.80.0070.01015.64
7.1.70.0060.00616.31
7.1.60.0150.00517.63
7.1.50.0130.01325.45
7.1.40.0070.00715.91
7.1.30.0000.01415.68
7.1.20.0040.01115.76
7.1.10.0070.00715.94
7.1.00.0020.04018.99
7.0.330.0030.01315.27
7.0.320.0060.00615.54
7.0.310.0060.00315.52
7.0.300.0070.00715.34
7.0.290.0060.00915.41
7.0.280.0060.00915.46
7.0.270.0060.00615.36
7.0.260.0030.00915.49
7.0.250.0030.01215.58
7.0.240.0060.00915.56
7.0.230.0040.01215.29
7.0.220.0080.00015.41
7.0.210.0000.01215.45
7.0.200.0030.01116.19
7.0.190.0040.01115.45
7.0.180.0030.00615.64
7.0.170.0030.01015.31
7.0.160.0060.00815.28
7.0.150.0070.00715.41
7.0.140.0100.03718.83
7.0.130.0050.00515.54
7.0.120.0060.00615.44
7.0.110.0040.00715.19
7.0.100.0060.00315.43
7.0.90.0060.00815.34
7.0.80.0100.00315.41
7.0.70.0080.04717.60
7.0.60.0070.04317.75
7.0.50.0130.04017.91
7.0.40.0100.04316.77
7.0.30.0070.05016.84
7.0.20.0140.03616.71
7.0.10.0070.02216.68
7.0.00.0070.02516.67
5.6.400.0040.00414.33
5.6.390.0040.01214.37
5.6.380.0110.00714.53
5.6.370.0070.00714.75
5.6.360.0060.00614.64
5.6.350.0030.00914.64
5.6.340.0140.00314.39
5.6.330.0030.00914.54
5.6.320.0050.00814.63
5.6.310.0060.01214.81
5.6.300.0090.00614.55
5.6.290.0000.00914.80
5.6.280.0070.03817.73
5.6.270.0040.00914.66
5.6.260.0000.01114.18
5.6.250.0000.01414.55
5.6.240.0090.00014.59
5.6.230.0000.00914.71
5.6.220.0100.03817.66
5.6.210.0100.04317.54
5.6.200.0050.04617.71
5.6.190.0030.05017.62
5.6.180.0110.04317.76
5.6.170.0070.04417.68
5.6.160.0040.04917.79
5.6.150.0160.03017.75
5.6.140.0040.04517.76
5.6.130.0100.04517.83
5.6.120.0020.04517.61
5.6.110.0140.03817.87
5.6.100.0050.04917.70
5.6.90.0120.04517.92
5.6.80.0070.04217.46
5.6.70.0120.04017.39
5.6.60.0030.03917.37
5.6.50.0050.04817.35
5.6.40.0060.04417.42
5.6.30.0120.04417.55
5.6.20.0040.04917.39
5.6.10.0060.03617.28
5.6.00.0100.04417.43
5.5.380.0000.01714.39
5.5.370.0070.00714.48
5.5.360.0110.03717.47
5.5.350.0070.04817.42
5.5.340.0030.02817.62
5.5.330.0100.02817.57
5.5.320.0050.02717.61
5.5.310.0080.04717.56
5.5.300.0080.04217.62
5.5.290.0100.04317.62
5.5.280.0050.03717.56
5.5.270.0070.04517.57
5.5.260.0070.04017.63
5.5.250.0060.04817.46
5.5.240.0050.05017.28
5.5.230.0130.04217.27
5.5.220.0050.03017.12
5.5.210.0100.04117.29
5.5.200.0070.02917.31
5.5.190.0100.04317.21
5.5.180.0080.03617.26
5.5.170.0000.01114.33
5.5.160.0100.04017.33
5.5.150.0070.04317.29
5.5.140.0130.03417.29
5.5.130.0070.04017.36
5.5.120.0150.03217.30
5.5.110.0030.04917.19
5.5.100.0080.02817.22
5.5.90.0130.03217.38
5.5.80.0100.03017.15
5.5.70.0100.03717.29
5.5.60.0090.02717.24
5.5.50.0100.04017.28
5.5.40.0060.02717.41
5.5.30.0050.04517.23
5.5.20.0020.03517.25
5.5.10.0030.04817.42
5.5.00.0100.04517.23
5.4.450.0050.04315.63
5.4.440.0050.03915.65
5.4.430.0080.03215.60
5.4.420.0030.04715.67
5.4.410.0070.03815.66
5.4.400.0080.03815.45
5.4.390.0050.04515.46
5.4.380.0070.03715.51
5.4.370.0100.04015.48
5.4.360.0050.04115.40
5.4.350.0040.03815.53
5.4.340.0030.04615.48
5.4.330.0030.01011.83
5.4.320.0050.04015.56
5.4.310.0030.04015.51
5.4.300.0100.03515.51
5.4.290.0150.03515.42
5.4.280.0030.03015.39
5.4.270.0050.03715.46
5.4.260.0120.03715.38
5.4.250.0080.04315.48
5.4.240.0020.04715.46
5.4.230.0050.03315.35
5.4.220.0030.04715.46
5.4.210.0030.04515.39
5.4.200.0050.04215.39
5.4.190.0090.03515.46
5.4.180.0070.03715.48
5.4.170.0070.03815.42
5.4.160.0030.04515.53
5.4.150.0120.03915.51
5.4.140.0100.03814.17
5.4.130.0050.04014.04
5.4.120.0100.03514.07
5.4.110.0070.03214.21
5.4.100.0020.03414.19
5.4.90.0050.04214.19
5.4.80.0090.03714.13
5.4.70.0030.03214.08
5.4.60.0090.02014.14
5.4.50.0060.02214.15
5.4.40.0040.04214.21
5.4.30.0060.02914.13
5.4.20.0020.03414.18
5.4.10.0110.02614.14
5.4.00.0050.03713.80
5.3.290.0060.02613.26
5.3.280.0070.04413.23
5.3.270.0070.04313.25
5.3.260.0050.03113.25
5.3.250.0060.03513.21
5.3.240.0110.03713.29
5.3.230.0070.03913.23
5.3.220.0040.02513.25
5.3.210.0050.02813.19
5.3.200.0030.02813.17
5.3.190.0070.04213.26
5.3.180.0080.02513.21
5.3.170.0050.02813.23
5.3.160.0050.04313.20
5.3.150.0060.04213.22
5.3.140.0070.03713.16
5.3.130.0070.03713.19
5.3.120.0070.02513.15
5.3.110.0070.03913.17
5.3.100.0040.03912.95
5.3.90.0080.02312.97
5.3.80.0040.03612.97
5.3.70.0090.02312.95
5.3.60.0030.02512.95
5.3.50.0100.01812.90
5.3.40.0050.02912.92
5.3.30.0030.02312.81
5.3.20.0050.03912.71
5.3.10.0030.03712.69
5.3.00.0060.03912.78
5.2.170.0000.04711.01
5.2.160.0000.03311.13
5.2.150.0030.03011.05
5.2.140.0100.05311.03
5.2.130.0030.06711.20
5.2.120.0130.05711.01
5.2.110.0070.04711.00
5.2.100.0030.05011.26
5.2.90.0000.05711.02
5.2.80.0030.06011.16
5.2.70.0100.05711.21
5.2.60.0070.04310.92
5.2.50.0070.06011.03
5.2.40.0030.06311.13
5.2.30.0100.04310.98
5.2.20.0030.06310.96
5.2.10.0030.05710.77
5.2.00.0030.05710.72
5.1.60.0000.06010.49
5.1.50.0030.05310.49
5.1.40.0000.06010.49
5.1.30.0000.06010.49
5.1.20.0000.04310.49
5.1.10.0030.06010.49
5.1.00.0130.05010.49
5.0.50.0030.03710.49
5.0.40.0030.03010.49
5.0.30.0030.03710.49
5.0.20.0030.04710.49
5.0.10.0030.04310.49
5.0.00.0100.06710.49
4.4.90.0000.03710.49
4.4.80.0070.03310.49
4.4.70.0030.01710.49
4.4.60.0000.01710.49
4.4.50.0100.03010.49
4.4.40.0000.04310.49
4.4.30.0000.03010.49
4.4.20.0030.03710.49
4.4.10.0030.03310.49
4.4.00.0000.06010.49
4.3.110.0100.02010.49
4.3.100.0030.01710.49
4.3.90.0030.02310.49
4.3.80.0030.05310.49
4.3.70.0070.03010.49
4.3.60.0030.03710.49
4.3.50.0070.03010.49
4.3.40.0000.05310.49
4.3.30.0030.02310.49
4.3.20.0030.03710.49
4.3.10.0000.03710.49
4.3.00.0070.03010.49

preferences:
39.79 ms | 401 KiB | 5 Q