3v4l.org

run code in 300+ PHP versions simultaneously
<?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).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0130.00718.52
8.3.50.0140.00717.38
8.3.40.0120.00318.84
8.3.30.0120.00318.83
8.3.20.0030.00518.72
8.3.10.0090.00020.56
8.3.00.0000.00817.75
8.2.180.0150.00616.50
8.2.170.0100.00722.96
8.2.160.0100.00322.20
8.2.150.0040.00424.18
8.2.140.0040.00424.66
8.2.130.0030.00526.16
8.2.120.0080.00019.95
8.2.110.0100.01320.78
8.2.100.0040.00717.78
8.2.90.0090.00019.34
8.2.80.0000.00719.34
8.2.70.0040.00417.75
8.2.60.0080.00018.16
8.2.50.0000.00818.07
8.2.40.0000.00918.34
8.2.30.0040.00418.11
8.2.20.0050.00317.70
8.2.10.0000.00817.70
8.2.00.0000.00917.78
8.1.280.0090.00625.92
8.1.270.0040.00424.66
8.1.260.0050.00326.35
8.1.250.0050.00328.09
8.1.240.0140.00322.59
8.1.230.0040.00720.87
8.1.220.0000.00817.79
8.1.210.0090.00318.77
8.1.200.0080.00017.35
8.1.190.0050.00317.52
8.1.180.0040.00418.10
8.1.170.0000.00818.90
8.1.160.0040.00418.92
8.1.150.0080.00018.69
8.1.140.0060.00317.50
8.1.130.0000.00717.93
8.1.120.0040.00417.53
8.1.110.0050.00217.54
8.1.100.0000.00717.52
8.1.90.0070.00417.52
8.1.80.0000.00717.47
8.1.70.0070.00017.50
8.1.60.0030.00517.63
8.1.50.0040.00417.51
8.1.40.0030.00517.56
8.1.30.0000.00817.68
8.1.20.0000.00917.60
8.1.10.0040.00417.62
8.1.00.0080.00017.46
8.0.300.0000.00819.86
8.0.290.0000.00716.88
8.0.280.0000.00718.53
8.0.270.0040.00417.17
8.0.260.0040.00416.77
8.0.250.0000.00716.98
8.0.240.0040.00416.95
8.0.230.0070.00017.02
8.0.220.0030.00316.96
8.0.210.0040.00317.01
8.0.200.0040.00416.96
8.0.190.0070.00016.87
8.0.180.0050.00216.95
8.0.170.0080.00016.81
8.0.160.0000.00716.90
8.0.150.0040.00416.87
8.0.140.0040.00416.88
8.0.130.0060.00013.42
8.0.120.0000.00716.87
8.0.110.0060.00316.98
8.0.100.0050.00316.80
8.0.90.0000.00816.88
8.0.80.0100.00616.88
8.0.70.0040.00416.90
8.0.60.0040.00416.91
8.0.50.0000.00716.97
8.0.30.0140.00516.91
8.0.20.0150.00317.26
8.0.10.0070.00017.17
8.0.00.0090.00916.74
7.4.330.0050.00014.73
7.4.320.0060.00016.60
7.4.300.0000.00716.63
7.4.290.0000.00716.70
7.4.280.0040.00816.61
7.4.270.0000.00716.57
7.4.260.0030.00313.34
7.4.250.0040.00416.54
7.4.240.0070.00016.54
7.4.230.0000.00716.64
7.4.220.0120.00616.68
7.4.210.0090.00716.62
7.4.200.0030.00316.65
7.4.190.0080.00016.60
7.4.160.0060.00916.55
7.4.150.0060.01217.07
7.4.140.0060.01117.14
7.4.130.0070.01116.59
7.4.120.0070.01016.55
7.4.110.0110.00716.36
7.4.100.0110.00816.63
7.4.90.0080.01116.48
7.4.80.0070.01718.80
7.4.70.0060.00716.48
7.4.60.0070.01316.57
7.4.50.0060.00323.17
7.4.40.0000.01221.69
7.4.30.0030.01216.66
7.4.00.0050.00515.10
7.3.330.0030.00313.34
7.3.320.0000.00613.22
7.3.310.0070.00016.40
7.3.300.0070.00016.30
7.3.290.0030.01616.36
7.3.280.0110.00516.36
7.3.270.0150.00317.07
7.3.260.0130.01018.24
7.3.250.0050.01316.54
7.3.240.0030.01416.47
7.3.230.0110.00716.41
7.3.210.0120.00616.68
7.3.200.0090.01319.39
7.3.190.0120.00916.54
7.3.180.0060.00916.47
7.3.170.0000.02016.51
7.3.160.0060.00916.68
7.3.120.0050.00514.68
7.2.330.0120.00616.88
7.2.320.0140.00416.87
7.2.310.0090.00916.67
7.2.300.0120.00816.70
7.2.290.0160.00716.75
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

preferences:
43.32 ms | 401 KiB | 5 Q