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 &find_by_key($key, $array) { foreach($array as $k => $v) { if($key == $k) { return $v; } } foreach($array as $sub) { if(isset($sub['children']) && FALSE !== ($found = &find_by_key($key, $sub['children']))) { return $found; } } return FALSE; } function v2($array) { $out = array(); foreach ($array as $key => $sub) { if(!isset($sub['parent_id'])) { $out[$key] = $sub; } else { $parent = &find_by_key($sub['parent_id'], $out); $parent['children'][$key] = $sub; return $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.70.0130.01018.43
8.3.60.0030.01218.31
8.3.50.0130.00522.11
8.3.40.0120.00918.65
8.3.30.0160.00618.43
8.3.20.0040.00418.82
8.3.10.0080.00021.23
8.3.00.0080.00020.81
8.2.180.0110.00716.75
8.2.170.0040.01122.96
8.2.160.0100.00320.52
8.2.150.0050.00324.18
8.2.140.0080.00024.66
8.2.130.0030.00626.16
8.2.120.0060.00921.05
8.2.110.0090.00019.18
8.2.100.0110.00017.91
8.2.90.0040.00819.13
8.2.80.0050.00317.97
8.2.70.0040.00417.50
8.2.60.0000.00817.93
8.2.50.0090.00018.07
8.2.40.0050.00318.09
8.2.30.0040.00421.24
8.2.20.0000.00817.68
8.2.10.0050.00217.79
8.2.00.0000.00817.70
8.1.280.0110.00425.92
8.1.270.0070.00023.99
8.1.260.0040.01126.35
8.1.250.0000.00728.09
8.1.240.0050.00521.39
8.1.230.0160.00017.45
8.1.220.0040.00417.78
8.1.210.0040.00418.77
8.1.200.0100.00017.35
8.1.190.0060.00317.23
8.1.180.0040.00418.10
8.1.170.0000.00818.46
8.1.160.0050.00218.86
8.1.150.0000.00718.61
8.1.140.0040.00417.53
8.1.130.0000.00717.93
8.1.120.0040.00417.51
8.1.110.0040.00417.45
8.1.100.0000.00717.41
8.1.90.0040.00417.42
8.1.80.0040.00417.55
8.1.70.0030.00317.39
8.1.60.0000.00917.61
8.1.50.0080.00017.55
8.1.40.0040.00417.59
8.1.30.0000.00817.60
8.1.20.0060.00317.56
8.1.10.0050.00517.57
8.1.00.0050.00317.53
8.0.300.0040.00418.77
8.0.290.0040.00416.58
8.0.280.0050.00218.48
8.0.270.0040.00417.26
8.0.260.0000.00716.90
8.0.250.0040.00417.02
8.0.240.0030.00517.01
8.0.230.0000.00716.95
8.0.220.0000.00716.83
8.0.210.0000.00816.90
8.0.200.0060.00016.85
8.0.190.0060.00316.95
8.0.180.0000.00716.85
8.0.170.0000.00916.96
8.0.160.0000.00816.82
8.0.150.0070.00016.84
8.0.140.0080.00016.90
8.0.130.0030.00313.39
8.0.120.0050.00316.90
8.0.110.0040.00416.98
8.0.100.0040.00416.86
8.0.90.0040.00416.93
8.0.80.0030.01316.94
8.0.70.0050.00217.02
8.0.60.0050.00216.95
8.0.50.0000.00716.88
8.0.30.0120.00816.92
8.0.20.0120.01417.40
8.0.10.0040.00416.92
8.0.00.0100.01416.76
7.4.330.0060.00016.79
7.4.320.0060.00016.64
7.4.300.0000.00616.61
7.4.290.0040.00416.64
7.4.280.0060.00016.64
7.4.270.0000.00716.55
7.4.260.0030.00313.27
7.4.250.0050.00316.40
7.4.240.0030.00316.68
7.4.230.0080.00016.71
7.4.220.0090.00916.49
7.4.210.0060.01016.62
7.4.200.0000.00716.43
7.4.190.0070.00016.78
7.4.160.0060.00916.46
7.4.150.0090.01217.40
7.4.140.0120.00917.86
7.4.130.0100.00716.47
7.4.120.0100.01216.70
7.4.110.0070.01016.30
7.4.100.0110.00516.61
7.4.90.0070.01116.57
7.4.80.0140.00419.39
7.4.70.0070.01016.60
7.4.60.0060.01116.45
7.4.50.0000.00916.39
7.4.40.0110.00322.77
7.4.30.0120.01216.48
7.4.00.0090.00614.90
7.3.330.0030.00313.35
7.3.320.0030.00313.15
7.3.310.0000.00616.27
7.3.300.0030.00516.33
7.3.290.0000.01416.26
7.3.280.0070.00916.37
7.3.270.0090.00917.40
7.3.260.0100.00818.24
7.3.250.0110.00616.65
7.3.240.0120.01016.43
7.3.230.0070.01016.60
7.3.210.0030.01316.46
7.3.200.0030.01619.39
7.3.190.0090.01216.28
7.3.180.0080.00816.26
7.3.170.0070.01416.38
7.3.160.0080.00816.51
7.3.120.0070.01014.61
7.3.10.0040.00816.61
7.3.00.0040.00816.72
7.2.330.0120.00916.83
7.2.320.0070.01116.84
7.2.310.0130.01016.64
7.2.300.0060.01616.48
7.2.290.0100.00616.80
7.2.130.0080.00817.00
7.2.120.0080.00416.77
7.2.110.0000.01416.97
7.2.100.0060.00316.84
7.2.90.0000.01017.18
7.2.80.0080.00417.07
7.2.70.0130.00016.82
7.2.60.0060.00616.59
7.2.50.0090.00916.96
7.2.40.0060.00317.02
7.2.30.0030.00717.00
7.2.20.0030.01016.97
7.2.10.0000.01316.75
7.2.00.0080.00417.02
7.1.250.0090.00315.97
7.1.200.0090.00315.88
7.1.100.0040.00818.29
7.1.70.0040.00416.93
7.1.60.0130.00317.28
7.1.50.0060.00817.04
7.1.40.0030.01716.76
7.1.30.0060.00616.52
7.1.20.0130.00716.74
7.1.10.0030.00716.61
7.1.00.0000.01016.82
7.0.200.0000.00816.83
7.0.190.0040.00716.42
7.0.180.0040.00716.36
7.0.170.0000.01016.09
7.0.160.0030.01016.46
7.0.150.0070.00316.49
7.0.140.0000.01116.43
7.0.130.0040.00416.46
7.0.120.0000.00816.68
7.0.110.0030.00516.46
7.0.100.0120.00016.33
7.0.90.0040.00416.30
7.0.80.0070.00416.63
7.0.70.0000.01116.20
7.0.60.0000.00715.99
7.0.50.0030.00516.29
7.0.40.0080.00614.86
7.0.30.0030.00314.86
7.0.20.0030.00914.86
7.0.10.0060.01014.86
7.0.00.0030.01014.86
5.6.300.0000.05220.84
5.6.290.0100.07120.91
5.6.280.0030.08820.88
5.6.270.0030.05420.98
5.6.260.0130.03920.89
5.6.250.0030.07120.97
5.6.240.0070.03920.89
5.6.230.0070.07920.84
5.6.220.0030.04520.97
5.6.210.0090.03821.07
5.6.200.0070.04120.95
5.6.190.0230.04020.86
5.6.180.0130.04120.90
5.6.170.0070.04420.96
5.6.160.0070.05720.94
5.6.150.0030.04520.95
5.6.140.0030.04320.73
5.6.130.0030.07521.05
5.6.120.0030.05420.93
5.6.110.0060.05420.82
5.6.100.0030.04120.79
5.6.90.0070.05020.84
5.6.80.0030.08320.41
5.6.70.0000.05420.32
5.6.60.0070.04920.40
5.6.50.0040.03920.20
5.6.40.0000.05020.41
5.6.30.0090.03420.20
5.6.20.0030.03820.18
5.6.10.0030.04120.20
5.6.00.0030.04720.28
5.5.380.0000.04217.46
5.5.370.0030.04917.54
5.5.360.0030.04217.64
5.5.350.0070.03817.56
5.5.340.0000.04518.09
5.5.330.0000.04618.01
5.5.320.0130.03618.10
5.5.310.0000.04318.09
5.5.300.0030.04418.05
5.5.290.0060.04218.09
5.5.280.0030.04918.02
5.5.270.0000.04518.02
5.5.260.0030.07318.17
5.5.250.0100.03517.95
5.5.240.0000.04517.48
5.5.230.0070.03617.55
5.5.220.0070.03717.38
5.5.210.0040.03917.43
5.5.200.0030.04017.39
5.5.190.0030.04317.39
5.5.180.0030.04117.35
5.5.160.0030.03717.48
5.5.150.0030.04917.46
5.5.140.0000.05417.45
5.5.130.0030.03717.38
5.5.120.0060.03917.35
5.5.110.0000.04317.45
5.5.100.0100.03217.38
5.5.90.0000.04717.25
5.5.80.0030.04616.96
5.5.70.0030.04417.37
5.5.60.0100.03717.33
5.5.50.0060.04216.98
5.5.40.0070.03717.34
5.5.30.0060.03917.36
5.5.20.0060.03817.08
5.5.10.0030.04017.37
5.5.00.0060.03917.09
5.4.450.0030.04319.54
5.4.440.0060.04619.46
5.4.430.0070.03919.41
5.4.420.0100.03619.46
5.4.410.0090.03219.36
5.4.400.0030.04019.18
5.4.390.0030.04419.14
5.4.380.0110.03519.14
5.4.370.0230.04618.98
5.4.360.0070.04619.09
5.4.350.0030.04219.13
5.4.340.0100.03419.21
5.4.320.0000.04619.11
5.4.310.0110.03219.11
5.4.300.0030.04219.23
5.4.290.0070.07818.89
5.4.280.0000.05219.09
5.4.270.0030.04119.16
5.4.260.0070.05119.12
5.4.250.0000.04419.11
5.4.240.0030.04319.09
5.4.230.0100.03219.10
5.4.220.0100.03819.12
5.4.210.0030.03819.20
5.4.200.0090.03919.07
5.4.190.0100.05519.08
5.4.180.0030.04318.98
5.4.170.0070.03419.17
5.4.160.0070.03719.05
5.4.150.0000.04519.05
5.4.140.0030.04616.75
5.4.130.0030.04416.65
5.4.120.0030.07616.80
5.4.110.0030.06516.78
5.4.100.0030.06816.78
5.4.90.0030.04116.71
5.4.80.0060.03216.80
5.4.70.0030.04316.79
5.4.60.0030.08316.71
5.4.50.0000.03816.68
5.4.40.0030.03716.65
5.4.30.0060.03916.73
5.4.20.0000.07016.60
5.4.10.0000.04116.68
5.4.00.0070.06915.89
5.3.290.0000.04214.86
5.3.280.0170.04714.86
5.3.270.0070.03714.86
5.3.260.0000.04614.90
5.3.250.0070.04814.86
5.3.240.0000.03814.86
5.3.230.0000.04114.86
5.3.220.0070.04114.86
5.3.210.0070.06614.86
5.3.200.0100.07214.86
5.3.190.0030.05814.87
5.3.180.0070.05714.86
5.3.170.0070.05514.86
5.3.160.0030.03714.86
5.3.150.0300.03314.86
5.3.140.0030.04314.86
5.3.130.0070.03314.86
5.3.120.0030.05814.86
5.3.110.0040.03914.86
5.3.100.0000.04214.86
5.3.90.0060.05114.86
5.3.80.0040.03914.86
5.3.70.0200.04014.86
5.3.60.0030.03814.86
5.3.50.0030.03814.86
5.3.40.0070.06914.86
5.3.30.0000.04914.86
5.3.20.0030.04214.86
5.3.10.0100.03314.86
5.3.00.0000.04814.86
5.2.170.0000.03214.86
5.2.160.0030.02914.86
5.2.150.0030.03814.86
5.2.140.0000.03214.86
5.2.130.0000.03314.86
5.2.120.0030.03714.86
5.2.110.0160.02314.86
5.2.100.0060.05214.86
5.2.90.0040.02514.86
5.2.80.0030.03914.86
5.2.70.0040.02814.86
5.2.60.0030.03914.86
5.2.50.0000.07814.86
5.2.40.0000.03014.86
5.2.30.0030.02814.86
5.2.20.0030.02714.86
5.2.10.0030.03114.86
5.2.00.0000.02914.86
5.1.60.0030.02414.86
5.1.50.0070.04014.86
5.1.40.0030.03114.86
5.1.30.0000.02814.86
5.1.20.0090.01914.86
5.1.10.0030.02714.86
5.1.00.0030.02714.86
5.0.50.0000.02914.86
5.0.40.0040.01814.86
5.0.30.0060.03114.86
5.0.20.0090.03114.86
5.0.10.0000.02414.86
5.0.00.0000.03314.86
4.4.90.0000.01614.86
4.4.80.0030.01314.86
4.4.70.0030.01314.86
4.4.60.0060.02814.86
4.4.50.0030.01414.86
4.4.40.0090.02214.86
4.4.30.0000.02314.86
4.4.20.0000.01814.86
4.4.10.0000.01914.86
4.4.00.0030.02514.86
4.3.110.0030.02514.86
4.3.100.0030.01314.86
4.3.90.0000.01714.86
4.3.80.0000.02714.86
4.3.70.0000.02014.86
4.3.60.0030.01414.86
4.3.50.0000.01714.86
4.3.40.0000.02514.86
4.3.30.0000.01914.86
4.3.20.0000.01914.86
4.3.10.0000.02214.86
4.3.00.0000.01814.86

preferences:
44.19 ms | 401 KiB | 5 Q