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); echo(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.10.0070.01014.94
7.4.00.0100.00714.92
7.3.130.0060.00914.80
7.3.120.0060.01314.92
7.3.110.0120.00814.79
7.3.100.0120.00314.61
7.3.90.0090.00615.17
7.3.80.0110.00415.01
7.3.70.0040.01115.00
7.3.60.0040.00814.95
7.3.50.0070.00314.98
7.3.40.0030.01414.78
7.3.30.0030.01014.70
7.3.20.0070.00716.78
7.3.10.0060.00916.76
7.3.00.0030.01016.71
7.2.260.0070.01314.73
7.2.250.0070.01114.97
7.2.240.0030.01214.98
7.2.230.0040.00715.01
7.2.220.0130.00314.74
7.2.210.0030.01015.05
7.2.200.0090.00614.97
7.2.190.0120.00415.19
7.2.180.0040.01115.17
7.2.170.0130.00315.21
7.2.160.0090.00614.94
7.2.150.0060.00616.74
7.2.140.0030.00816.88
7.2.130.0060.00916.83
7.2.120.0040.01117.01
7.2.110.0070.00716.73
7.2.100.0100.00617.20
7.2.90.0040.01116.98
7.2.80.0130.00316.98
7.2.70.0060.00917.05
7.2.60.0100.00016.87
7.2.50.0070.00717.01
7.2.40.0100.00717.14
7.2.30.0060.01016.75
7.2.20.0060.01216.98
7.2.10.0090.00616.90
7.2.00.0080.00718.31
7.1.330.0060.00916.05
7.1.320.0040.01115.93
7.1.310.0070.00715.75
7.1.300.0070.00715.91
7.1.290.0000.00815.68
7.1.280.0080.00015.81
7.1.270.0060.01015.53
7.1.260.0100.00315.77
7.1.250.0040.01215.82
7.1.240.0040.01215.79
7.1.230.0080.00815.80
7.1.220.0030.01316.07
7.1.210.0060.00715.76
7.1.200.0070.00416.04
7.1.190.0000.01315.91
7.1.180.0000.01516.01
7.1.170.0080.00815.55
7.1.160.0080.00415.81
7.1.150.0100.00315.58
7.1.140.0100.00015.93
7.1.130.0040.00815.94
7.1.120.0130.00715.70
7.1.110.0070.00315.58
7.1.100.0040.01115.88
7.1.90.0030.01316.01
7.1.80.0030.01016.01
7.1.70.0060.00516.42
7.1.60.0110.00817.54
7.1.50.0050.01216.41
7.1.40.0070.00715.61
7.1.30.0120.00315.46
7.1.20.0090.00015.88
7.1.10.0040.01115.73
7.1.00.0030.02219.03
7.0.330.0030.00915.36
7.0.320.0000.00815.47
7.0.310.0030.01115.44
7.0.300.0040.01115.60
7.0.290.0040.01115.47
7.0.280.0030.00615.48
7.0.270.0000.01515.63
7.0.260.0080.01215.43
7.0.250.0040.00715.33
7.0.240.0030.01315.40
7.0.230.0060.00615.43
7.0.220.0070.01015.30
7.0.210.0030.01215.23
7.0.200.0060.00215.89
7.0.190.0000.01615.24
7.0.180.0030.01015.36
7.0.170.0110.00415.58
7.0.160.0100.00315.31
7.0.150.0070.01015.50
7.0.140.0060.03818.72
7.0.130.0000.01115.04
7.0.120.0120.00015.50
7.0.110.0050.03217.72
7.0.100.0130.03617.75
7.0.90.0060.02817.68
7.0.80.0050.04417.74
7.0.70.0100.04017.63
7.0.60.0110.04017.66
7.0.50.0010.03617.89
7.0.40.0050.04516.81
7.0.30.0050.04616.98
7.0.20.0050.04516.79
7.0.10.0030.04416.76
7.0.00.0020.03316.78
5.6.400.0090.00314.50
5.6.390.0040.01114.48
5.6.380.0110.00014.69
5.6.370.0070.00714.23
5.6.360.0040.01114.57
5.6.350.0080.00414.66
5.6.340.0000.01414.45
5.6.330.0110.00414.74
5.6.320.0030.01014.69
5.6.310.0120.00314.47
5.6.300.0040.01114.12
5.6.290.0040.00414.79
5.6.280.0030.04217.61
5.6.270.0080.00814.75
5.6.260.0080.03617.57
5.6.250.0070.04417.46
5.6.240.0050.04517.54
5.6.230.0050.04717.55
5.6.220.0090.04117.40
5.6.210.0070.02817.41
5.6.200.0100.04017.92
5.6.190.0100.03817.93
5.6.180.0120.03917.69
5.6.170.0070.04317.96
5.6.160.0110.03917.88
5.6.150.0080.04117.67
5.6.140.0030.04217.73
5.6.130.0070.04717.65
5.6.120.0070.04717.65
5.6.110.0110.04317.84
5.6.100.0080.04317.84
5.6.90.0050.04717.51
5.6.80.0050.04517.32
5.6.70.0050.05017.43
5.6.60.0100.02317.34
5.6.50.0110.03817.38
5.6.40.0020.05217.41
5.6.30.0100.04017.43
5.6.20.0050.03017.35
5.6.10.0070.04317.38
5.6.00.0110.03817.27
5.5.380.0090.04117.37
5.5.370.0050.03017.33
5.5.360.0100.02917.47
5.5.350.0030.05217.32
5.5.340.0020.04717.57
5.5.330.0110.03617.52
5.5.320.0030.04817.61
5.5.310.0060.04217.64
5.5.300.0140.04217.58
5.5.290.0070.04617.56
5.5.280.0050.04417.59
5.5.270.0120.04017.57
5.5.260.0070.04617.60
5.5.250.0130.02717.44
5.5.240.0100.03217.32
5.5.230.0100.04217.36
5.5.220.0080.04017.09
5.5.210.0070.04217.43
5.5.200.0040.04417.23
5.5.190.0080.04317.23
5.5.180.0130.03517.28
5.5.170.0120.00014.12
5.5.160.0050.04517.18
5.5.150.0070.04017.29
5.5.140.0070.03417.35
5.5.130.0100.04217.23
5.5.120.0020.04917.21
5.5.110.0070.03417.26
5.5.100.0050.04317.21
5.5.90.0060.03917.18
5.5.80.0070.04117.36
5.5.70.0070.04517.11
5.5.60.0090.02917.25
5.5.50.0090.03517.12
5.5.40.0080.04217.14
5.5.30.0050.03817.29
5.5.20.0030.03317.13
5.5.10.0050.02617.30
5.5.00.0030.04417.29
5.4.450.0050.02715.09
5.4.440.0090.03315.60
5.4.430.0020.03915.20
5.4.420.0100.02315.05
5.4.410.0080.03515.27
5.4.400.0030.03915.32
5.4.390.0020.04515.07
5.4.380.0030.04415.06
5.4.370.0100.03514.88
5.4.360.0050.04315.26
5.4.350.0080.03715.14
5.4.340.0080.03215.24
5.4.330.0060.00011.01
5.4.320.0050.04314.95
5.4.310.0050.03915.15
5.4.300.0080.02515.21
5.4.290.0060.04015.18
5.4.280.0070.02415.14
5.4.270.0070.04215.16
5.4.260.0030.04315.20
5.4.250.0110.03815.15
5.4.240.0020.04515.24
5.4.230.0100.03715.15
5.4.220.0030.04215.18
5.4.210.0110.03315.12
5.4.200.0050.02315.06
5.4.190.0050.04215.03
5.4.180.0090.04015.17
5.4.170.0080.03815.06
5.4.160.0060.02614.98
5.4.150.0030.03615.20
5.4.140.0070.02013.69
5.4.130.0070.02713.79
5.4.120.0030.04013.80
5.4.110.0050.03313.93
5.4.100.0030.04013.78
5.4.90.0090.03613.80
5.4.80.0020.04513.96
5.4.70.0080.04013.90
5.4.60.0030.02913.79
5.4.50.0040.02913.88
5.4.40.0040.04413.93
5.4.30.0050.04213.88
5.4.20.0060.03813.83
5.4.10.0060.03813.90
5.4.00.0030.04013.59
5.3.290.0050.04012.53
5.3.280.0020.04112.38
5.3.270.0050.04112.37
5.3.260.0120.03712.58
5.3.250.0080.03012.45
5.3.240.0050.02712.43
5.3.230.0080.03612.43
5.3.220.0020.04312.42
5.3.210.0000.04512.41
5.3.200.0060.02212.48
5.3.190.0070.04012.57
5.3.180.0050.03712.57
5.3.170.0050.03812.50
5.3.160.0060.03712.48
5.3.150.0100.03512.51
5.3.140.0070.04312.47
5.3.130.0070.04112.41
5.3.120.0030.04312.46
5.3.110.0030.04112.47
5.3.100.0090.02212.28
5.3.90.0080.03712.26
5.3.80.0030.04312.21
5.3.70.0030.04312.07
5.3.60.0030.04112.20
5.3.50.0060.04012.14
5.3.40.0050.02512.12
5.3.30.0070.03112.15
5.3.20.0050.03512.02
5.3.10.0060.03811.99
5.3.00.0030.04011.95
5.2.170.0070.05311.92
5.2.160.0070.05711.92
5.2.150.0100.05311.92
5.2.140.0030.06311.92
5.2.130.0100.06011.92
5.2.120.0070.05711.92
5.2.110.0070.03311.92
5.2.100.0070.05711.92
5.2.90.0070.03311.92
5.2.80.0100.03011.92
5.2.70.0100.05711.92
5.2.60.0030.05711.92
5.2.50.0030.06711.92
5.2.40.0070.06011.92
5.2.30.0000.06711.92
5.2.20.0070.06311.92
5.2.10.0100.05711.92
5.2.00.0100.05311.92
5.1.60.0070.05011.92
5.1.50.0100.03011.92
5.1.40.0070.02711.92
5.1.30.0070.05011.92
5.1.20.0030.05011.92
5.1.10.0030.06011.92
5.1.00.0100.05011.92
5.0.50.0030.04711.92
5.0.40.0030.04311.92
5.0.30.0070.05311.92
5.0.20.0070.03311.92
5.0.10.0030.04311.92
5.0.00.0070.05711.92
4.4.90.0070.02711.92
4.4.80.0000.02311.92
4.4.70.0000.03711.92
4.4.60.0000.04011.92
4.4.50.0000.02711.92
4.4.40.0070.04711.92
4.4.30.0000.03711.92
4.4.20.0100.01311.92
4.4.10.0030.03311.92
4.4.00.0000.05311.92
4.3.110.0030.03311.92
4.3.100.0000.03711.92
4.3.90.0070.02711.92
4.3.80.0030.04011.92
4.3.70.0000.03311.92
4.3.60.0030.03311.92
4.3.50.0030.03711.92
4.3.40.0070.04711.92
4.3.30.0000.03711.92
4.3.20.0070.01311.92
4.3.10.0030.02711.92
4.3.00.0070.01311.92

preferences:
32.67 ms | 401 KiB | 5 Q