3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $array = array( 55 => array( 'ident' => 'test 1', 'depth' => 1, ), 77 => array( 'parent_id' => 55, 'ident' => 'test 2', 'depth' => 2, ), 109 => array( 'parent_id' => 77, 'ident' => 'test 3', 'depth' => 3, ), 78 => array( 'parent_id' => 55, 'ident' => 'test 4', 'depth' => 2, ), 25 => array( 'ident' => 'test 5', 'depth' => 1, ) ); function v1($array) { foreach ($array as $key => &$sub) { if (isset($sub['parent_id'])) { $array[$sub['parent_id']]['children'][$key] = &$sub; } } unset($sub); // unset the reference to make sure to not overwrite it later... // now remove the entries with parents foreach ($array as $key => $sub) { if (isset($sub['parent_id'])) unset($array[$key]); } return $array; } function push_at_key($what, $what_key, $key, $array) { foreach($array as $k => $v) { if($key == $k) { $array[$k]['children'][$what_key] = $what; return $array; } } foreach($array as $sub) { if(isset($sub['children']) && FALSE !== ($found = push_at_key($what, $what_key, $key, $sub['children']))) { return $found; } } return $array; } function v2($array) { $out = array(); foreach ($array as $key => $sub) { if(!isset($sub['parent_id'])) { $out[$key] = $sub; } else { $out = push_at_key($sub, $key, $sub['parent_id'], $out); } } return $out; } //var_dump(v1($array)); var_dump(v2($array));

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).
Note: hhvm uses a JIT which only kicks in after a few runs. Since we run scripts only once, hhvm always runs slower.

VersionSystem time (s)User time (s)Memory (MiB)
7.2.00.0030.00619.55
7.1.100.0030.01018.17
7.1.70.0070.01117.20
7.1.60.0070.01319.40
7.1.50.0180.01834.60
7.1.00.0000.07722.36
7.0.200.0040.00416.96
7.0.140.0070.07022.12
7.0.120.0030.03022.09
7.0.60.0000.07019.82
7.0.50.0030.07017.91
7.0.40.0100.03720.11
7.0.30.0370.07320.30
7.0.20.0270.07720.10
7.0.10.0030.05020.19
7.0.00.0230.04320.16
5.6.280.0030.06721.02
5.6.210.0100.08020.72
5.6.200.0000.05018.24
5.6.190.0070.04320.42
5.6.180.4070.05020.33
5.6.170.0300.03020.47
5.6.160.0070.06720.46
5.6.150.0130.07318.18
5.6.140.0100.03318.18
5.6.130.0030.04318.21
5.6.120.0100.04321.03
5.6.110.0130.07721.07
5.6.100.0170.06321.14
5.6.90.0200.06320.86
5.6.80.0070.08020.45
5.6.70.4330.03320.39
5.5.350.3470.03020.44
5.5.340.0170.07717.99
5.5.330.0030.08320.29
5.5.320.2800.04020.24
5.5.310.0100.04720.37
5.5.300.0070.08317.96
5.5.290.0130.07318.04
5.5.280.0030.04020.67
5.5.270.0070.04720.78
5.5.260.0070.08020.88
5.5.250.0130.08020.81
5.5.240.0270.07020.08
5.4.450.2800.04019.39
5.4.440.0470.03019.39
5.4.430.0270.04719.38
5.4.420.0200.04319.33
5.4.410.0270.07019.07
5.4.400.0200.05019.12
5.4.390.0230.07019.16
5.4.380.0300.06018.86
5.4.370.0300.06718.87
5.4.360.0200.04319.08
5.4.350.0330.06019.19
5.4.340.0300.04019.19
5.4.320.0300.06718.88
5.4.310.0230.06018.87
5.4.300.0200.07019.18
5.4.290.0270.07019.18
5.4.280.0270.03718.99
5.4.270.0200.06318.98
5.4.260.0270.04319.23
5.4.250.0230.03719.07
5.4.240.0230.04319.23
5.4.230.0200.04019.05
5.4.220.0270.04319.27
5.4.210.0270.07319.27
5.4.200.0370.06718.97
5.4.190.0200.04318.84
5.4.180.0270.05718.84
5.4.170.0230.05019.18
5.4.160.0270.03719.14
5.4.150.0300.07319.26
5.4.140.0430.03716.45
5.4.130.0270.05316.55
5.4.120.0270.04016.54
5.4.110.0170.04316.33
5.4.100.0300.06716.50
5.4.90.0200.04716.59
5.4.80.0300.05716.39
5.4.70.0230.04316.55
5.4.60.0270.06316.23
5.4.50.0230.03716.19
5.4.40.0270.04716.24
5.4.30.0330.06716.52
5.4.20.0270.03316.47
5.4.10.0270.04316.52
5.4.00.0370.02315.73
5.3.290.0370.05314.68
5.3.280.0330.05714.83
5.3.270.0230.05314.74
5.3.260.0300.05314.74
5.3.250.0300.06014.63
5.3.240.0300.07314.61
5.3.230.0270.07314.66
5.3.220.0400.05714.50
5.3.210.0300.07314.67
5.3.200.0230.03714.49
5.3.190.0330.05714.73
5.3.180.0200.05014.66
5.3.170.0300.05314.49
5.3.160.0270.06014.48
5.3.150.0270.03714.58
5.3.140.0300.07014.59
5.3.130.0300.05714.79
5.3.120.0370.06314.57
5.3.110.0270.06014.49
5.3.100.0300.04314.05
5.3.90.0300.06314.25
5.3.80.0270.03314.19
5.3.70.0300.06714.08
5.3.60.0270.04714.13
5.3.50.0230.04014.12
5.3.40.0300.06713.96
5.3.30.0270.04314.14
5.3.20.0200.05013.61
5.3.10.0200.04713.78
5.3.00.0200.04313.88
5.2.170.0270.04711.28
5.2.160.0230.04311.12
5.2.150.0300.05311.21
5.2.140.0170.03311.18
5.2.130.0230.05710.98
5.2.120.0200.06011.03
5.2.110.0170.03311.02
5.2.100.0200.05011.04
5.2.90.0270.05311.05
5.2.80.0200.04311.02
5.2.70.0200.03011.07
5.2.60.0270.05711.14
5.2.50.0270.05711.09
5.2.40.0200.04310.91
5.2.30.0200.03010.91
5.2.20.0230.05710.87
5.2.10.0200.05010.84
5.2.00.0200.04710.66
5.1.60.0130.03010.10
5.1.50.0230.05010.14
5.1.40.0130.0539.98
5.1.30.0200.02710.40
5.1.20.0170.03010.28
5.1.10.0270.0439.98
5.1.00.0130.0309.97
5.0.50.0130.0478.63
5.0.40.0170.0308.60
5.0.30.0070.0338.15
5.0.20.0070.0478.16
5.0.10.0030.0278.43
5.0.00.0070.0638.13
4.4.90.0070.0177.69
4.4.80.0100.0177.69
4.4.70.0100.0377.69
4.4.60.0170.0377.69
4.4.50.0170.0307.69
4.4.40.0100.0507.69
4.4.30.0130.0207.69
4.4.20.0100.0177.69
4.4.10.0070.0377.69
4.4.00.0070.0377.69
4.3.110.0070.0207.69
4.3.100.0100.0177.69
4.3.90.0100.0237.69
4.3.80.0100.0507.69
4.3.70.0030.0177.69
4.3.60.0070.0177.69
4.3.50.0070.0177.69
4.3.40.0070.0277.69
4.3.30.0100.0307.69
4.3.20.0030.0277.69
4.3.10.0030.0377.69
4.3.00.0030.0337.69