3v4l.org

run code in 300+ PHP versions simultaneously
<?php function getTree($array) { $level = 0; $tree = []; $stack = [uniqid(1) => &$tree]; //why uniqid? Try do debug without that :p foreach($array as $item) { if($item['level']>$level) //expand stack for new items { //if there are child elements, add last to stack: $top = key($stack); if(count($stack[$top])) { end($stack[$top]); $stack[uniqid()] = &$stack[$top][key($stack[$top])]; } //add ['children'] dim to top stack element end($stack); $top = key($stack); $stack[$top]['children'] = []; $stack[uniqid(1)] = &$stack[$top]['children']; //add current item as a first child of top stack element end($stack); $top = key($stack); $stack[$top][] = $item; } elseif($item['level']<$level) //pop stack items till corresponding level { while($item['level']<$level--) { //two times: one for last pointer, one for ['children'] dim array_pop($stack); array_pop($stack); } end($stack); $stack[key($stack)][] = $item; } elseif($item['level']==$level) //add to current level { //just add since top stack element will be ['children'] dim $stack[key($stack)][] = $item; } end($stack); $level = $item['level']; } return $tree; } $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'=>2, 'name' => 'subroot 2-2'], ['level'=>1, 'name' => 'Root #3'] ]; echo('<pre>'); $result = getTree($array); print_r($result);

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.1.70.0000.00717.31
7.1.60.0120.01219.17
7.1.50.0040.01917.07
7.1.00.0070.07322.44
7.0.200.0070.00716.46
7.0.140.0100.07022.16
7.0.100.0070.07020.04
7.0.90.0030.08719.92
7.0.80.0270.08319.92
7.0.70.0070.06320.02
7.0.60.0030.09320.09
7.0.50.0130.07320.25
7.0.40.0170.07320.03
7.0.30.0100.08020.18
7.0.20.0130.08020.04
7.0.10.0170.07020.09
7.0.00.0100.07020.04
5.6.280.0100.06720.87
5.6.250.0130.07720.61
5.6.240.0130.07320.67
5.6.230.0030.07720.67
5.6.220.0170.08020.68
5.6.210.0130.04020.57
5.6.200.0070.08320.96
5.6.190.0170.07021.18
5.6.180.0170.07321.11
5.6.170.0170.04020.95
5.6.160.0030.05321.04
5.6.150.0130.07321.03
5.6.140.0200.07321.09
5.6.130.0130.07721.11
5.6.120.0030.06721.07
5.6.110.0130.03721.11
5.6.100.0230.07021.17
5.6.90.0030.04721.04
5.6.80.0200.06020.50
5.6.70.0070.08020.43
5.6.60.0130.07320.39
5.6.50.0100.06020.45
5.6.40.0000.05720.34
5.6.30.0070.07020.31
5.6.20.0070.04720.42
5.6.10.0100.07320.50
5.6.00.0170.06020.31
5.5.380.0170.05320.43
5.5.370.0100.06320.38
5.5.360.0100.05020.57
5.5.350.0130.07020.40
5.5.340.0170.08020.88
5.5.330.0100.07720.79
5.5.320.0100.07320.80
5.5.310.0200.06720.77
5.5.300.0030.05320.87
5.5.290.0130.08320.74
5.5.280.0200.07720.80
5.5.270.0100.04320.86
5.5.260.0200.07720.92
5.5.250.0070.06320.55
5.5.240.0070.08020.17
5.5.230.0130.07320.16
5.5.220.0070.08020.24
5.5.210.0270.04020.16
5.5.200.0000.05320.12
5.5.190.0100.07720.13
5.5.180.0030.08320.28
5.5.160.0200.06720.15
5.5.150.0030.05020.16
5.5.140.0070.07720.15
5.5.130.0030.07720.28
5.5.120.0230.06020.18
5.5.110.0030.05320.19
5.5.100.0030.08319.97
5.5.90.0030.07720.10
5.5.80.0130.07320.05
5.5.70.0130.06720.05
5.5.60.0030.06320.04
5.5.50.0070.07319.91
5.5.40.0030.08020.10
5.5.30.0170.06020.16
5.5.20.0030.04719.96
5.5.10.0030.05020.04
5.5.00.0030.04019.97
5.4.450.0100.07719.37
5.4.440.0170.06019.41
5.4.430.0070.08319.56
5.4.420.0070.04719.37
5.4.410.0000.04719.32
5.4.400.0100.07019.24
5.4.390.0030.08019.09
5.4.380.0070.07019.18
5.4.370.0100.07018.90
5.4.360.0100.06719.11
5.4.350.0030.04019.16
5.4.340.0070.07719.09
5.4.320.0070.07318.95
5.4.310.0030.07018.85
5.4.300.0130.03719.20
5.4.290.0170.03019.18
5.4.280.0070.07719.24
5.4.270.0100.08318.84
5.4.260.0100.08019.10
5.4.250.0000.05018.87
5.4.240.0000.05019.13
5.4.230.0070.05719.20
5.4.220.0100.07019.02
5.4.210.0170.02719.20
5.4.200.0070.04319.12
5.4.190.0000.07019.14
5.4.180.0170.05319.20
5.4.170.0070.04019.09
5.4.160.0130.06019.14
5.4.150.0170.05718.86
5.4.140.0130.06016.43
5.4.130.0100.06016.19
5.4.120.0100.05016.25
5.4.110.0030.07016.46
5.4.100.0100.07316.46
5.4.90.0130.07016.47
5.4.80.0100.03016.32
5.4.70.0130.06316.51
5.4.60.0100.06016.25
5.4.50.0170.06716.41
5.4.40.0170.06016.44
5.4.30.0100.06016.41
5.4.20.0070.06716.36
5.4.10.0000.08316.32
5.4.00.0070.07315.78
5.3.290.0030.04315.73
5.3.280.0100.03715.73
5.3.270.0030.06715.73
5.3.260.0100.06715.73
5.3.250.0000.03715.73
5.3.240.0070.03315.73
5.3.230.0100.06015.73
5.3.220.0100.05315.73
5.3.210.0030.04315.73
5.3.200.0030.04315.73
5.3.190.0100.06715.73
5.3.180.0070.07715.73
5.3.170.0000.04315.73
5.3.160.0100.06715.73
5.3.150.0070.07315.73
5.3.140.0100.06015.73
5.3.130.0130.06315.73
5.3.120.0070.07715.73
5.3.110.0100.03715.73
5.3.100.0070.04015.73
5.3.90.0000.04315.73
5.3.80.0000.07015.73
5.3.70.0070.07315.73
5.3.60.0130.07015.73
5.3.50.0030.07315.73
5.3.40.0000.07315.73
5.3.30.0030.05015.73
5.3.20.0030.05715.73
5.3.10.0230.03015.73
5.3.00.0070.04315.73
5.2.170.0070.05015.73
5.2.160.0070.05715.73
5.2.150.0030.05015.73
5.2.140.0030.05715.73
5.2.130.0070.05315.73
5.2.120.0100.05715.73
5.2.110.0070.06015.73
5.2.100.0030.03315.73
5.2.90.0100.04015.73
5.2.80.0100.06315.73
5.2.70.0030.03715.73
5.2.60.0000.04315.73
5.2.50.0030.05015.73
5.2.40.0070.05715.73
5.2.30.0000.03015.73
5.2.20.0000.04015.73
5.2.10.0100.05015.73
5.2.00.0000.06315.73
5.1.60.0030.02015.73
5.1.50.0000.05715.73
5.1.40.0030.02715.73
5.1.30.0000.06015.73
5.1.20.0030.06015.73
5.1.10.0100.05015.73
5.1.00.0070.04015.73
5.0.50.0030.05015.73
5.0.40.0070.04015.73
5.0.30.0030.06315.73
5.0.20.0000.04715.73
5.0.10.0000.04715.73
5.0.00.0100.04015.73
4.4.90.0030.01715.73
4.4.80.0000.01715.73
4.4.70.0000.03715.73
4.4.60.0030.02015.73
4.4.50.0000.01315.73
4.4.40.0030.04015.73
4.4.30.0030.03315.73
4.4.20.0000.03315.73
4.4.10.0070.01715.73
4.4.00.0000.05015.73
4.3.110.0030.02315.73
4.3.100.0030.02315.73
4.3.90.0000.02015.73
4.3.80.0000.05315.73
4.3.70.0030.03715.73
4.3.60.0070.03015.73
4.3.50.0070.03315.73
4.3.40.0070.05015.73
4.3.30.0000.02715.73
4.3.20.0000.02015.73
4.3.10.0000.02315.73
4.3.00.0000.03315.73

preferences:
29.81 ms | 401 KiB | 5 Q