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; } //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.0120.00618.56
8.3.50.0100.00722.05
8.3.40.0040.01418.96
8.3.30.0090.00618.70
8.3.20.0040.00418.90
8.3.10.0050.00321.21
8.3.00.0070.00320.70
8.2.180.0170.00316.62
8.2.170.0180.00418.88
8.2.160.0090.00622.96
8.2.150.0040.00424.18
8.2.140.0040.00424.66
8.2.130.0040.00426.16
8.2.120.0040.00420.98
8.2.110.0100.00020.43
8.2.100.0080.00317.84
8.2.90.0000.00919.17
8.2.80.0050.00317.97
8.2.70.0060.00617.63
8.2.60.0040.00418.04
8.2.50.0030.00618.07
8.2.40.0080.00018.28
8.2.30.0050.00321.10
8.2.20.0040.00417.81
8.2.10.0040.00417.84
8.2.00.0030.00517.88
8.1.280.0040.01425.92
8.1.270.0040.00423.99
8.1.260.0080.00026.35
8.1.250.0080.00028.09
8.1.240.0030.00722.59
8.1.230.0110.00020.97
8.1.220.0000.00818.77
8.1.210.0000.00818.85
8.1.200.0060.00317.36
8.1.190.0080.00017.42
8.1.180.0030.00618.10
8.1.170.0000.00818.65
8.1.160.0000.00718.95
8.1.150.0070.00018.66
8.1.140.0040.00417.35
8.1.130.0000.00717.89
8.1.120.0000.00717.50
8.1.110.0040.00417.39
8.1.100.0070.00017.39
8.1.90.0040.00417.50
8.1.80.0040.00417.47
8.1.70.0000.00717.44
8.1.60.0050.00317.62
8.1.50.0000.00817.37
8.1.40.0060.00317.60
8.1.30.0060.00317.57
8.1.20.0050.00317.69
8.1.10.0070.00417.40
8.1.00.0000.00817.49
8.0.300.0090.00020.09
8.0.290.0040.00417.04
8.0.280.0000.00718.47
8.0.270.0080.00017.32
8.0.260.0030.00316.88
8.0.250.0000.00717.06
8.0.240.0040.00417.06
8.0.230.0000.00816.94
8.0.220.0030.00316.88
8.0.210.0040.00417.00
8.0.200.0000.00716.93
8.0.190.0050.00317.05
8.0.180.0080.00016.84
8.0.170.0080.00016.96
8.0.160.0040.00417.05
8.0.150.0000.00716.79
8.0.140.0000.00816.96
8.0.130.0040.00413.39
8.0.120.0060.00316.96
8.0.110.0040.00417.00
8.0.100.0040.00416.80
8.0.90.0000.00716.89
8.0.80.0030.01616.93
8.0.70.0000.00716.81
8.0.60.0020.00516.89
8.0.50.0000.00817.02
8.0.30.0060.01217.00
8.0.20.0080.01217.40
8.0.10.0040.00417.09
8.0.00.0100.01116.77
7.4.330.0000.00516.73
7.4.320.0000.00616.61
7.4.300.0030.00316.63
7.4.290.0030.00516.42
7.4.280.0000.00716.56
7.4.270.0030.00316.53
7.4.260.0000.00513.24
7.4.250.0000.00716.64
7.4.240.0070.00016.68
7.4.230.0000.00716.73
7.4.220.0120.01616.54
7.4.210.0090.01016.53
7.4.200.0030.00316.46
7.4.190.0030.00516.49
7.4.160.0130.00616.57
7.4.150.0150.00817.40
7.4.140.0100.01017.86
7.4.130.0150.00516.68
7.4.120.0090.01116.52
7.4.110.0130.00416.68
7.4.100.0110.00716.38
7.4.90.0100.01416.68
7.4.80.0090.00919.39
7.4.70.0100.00716.71
7.4.60.0100.00716.49
7.4.50.0030.00616.39
7.4.40.0140.00322.77
7.4.30.0120.00416.47
7.4.00.0100.00715.01
7.3.330.0030.00413.27
7.3.320.0000.00613.20
7.3.310.0070.00016.48
7.3.300.0070.00016.32
7.3.290.0000.01616.48
7.3.280.0130.00616.45
7.3.270.0130.00617.40
7.3.260.0100.01118.24
7.3.250.0100.00716.34
7.3.240.0120.00416.44
7.3.230.0090.00916.48
7.3.210.0120.00616.40
7.3.200.0060.01119.39
7.3.190.0170.00316.50
7.3.180.0150.00316.42
7.3.170.0120.00416.54
7.3.160.0110.00516.61
7.3.120.0040.01515.12
7.2.330.0120.00616.49
7.2.320.0170.00016.61
7.2.310.0100.01016.49
7.2.300.0080.00816.70
7.2.290.0060.01016.83
7.2.110.0180.00416.79
7.2.60.0040.00816.76
7.2.00.0100.00719.15
7.1.200.0000.00915.74
7.1.70.0030.00317.38
7.1.60.0070.00317.37
7.1.50.0050.00517.15
7.1.40.0150.00916.95
7.1.30.0110.00316.95
7.1.20.0000.01216.95
7.1.10.0000.00916.95
7.1.00.0030.00916.95
7.0.200.0040.00416.95
7.0.190.0000.00816.95
7.0.180.0100.00316.95
7.0.170.0000.00816.95
7.0.160.0000.00916.95
7.0.150.0050.00216.95
7.0.140.0040.00716.95
7.0.130.0040.00416.95
7.0.120.0050.00516.95
7.0.110.0040.00716.95
7.0.100.0070.00016.95
7.0.90.0050.00316.95
7.0.80.0030.00516.95
7.0.70.0000.01116.95
7.0.60.0000.00716.95
7.0.50.0000.00816.95
7.0.40.0040.00416.95
7.0.30.0000.00816.95
7.0.20.0060.00916.95
7.0.10.0000.01516.95
7.0.00.0000.01016.95
5.6.300.0130.03220.84
5.6.290.0030.04320.82
5.6.280.0000.06120.89
5.6.270.0000.05020.91
5.6.260.0000.04621.07
5.6.250.0000.04220.86
5.6.240.0000.04720.87
5.6.230.0060.04120.82
5.6.220.0040.04220.96
5.6.210.0100.03821.13
5.6.200.0000.04820.78
5.6.190.0060.04520.78
5.6.180.0000.04320.94
5.6.170.0070.03521.04
5.6.160.0070.05720.74
5.6.150.0100.04520.84
5.6.140.0030.04420.74
5.6.130.0030.04221.03
5.6.120.0130.03120.93
5.6.110.0030.05020.98
5.6.100.0060.04221.04
5.6.90.0030.07820.81
5.6.80.0070.03620.39
5.6.70.0030.04420.28
5.6.60.0030.04320.23
5.6.50.0030.06620.40
5.6.40.0030.04720.29
5.6.30.0060.06120.34
5.6.20.0000.07020.41
5.6.10.0100.03520.09
5.6.00.0030.04520.40
5.5.380.0030.03717.44
5.5.370.0000.04517.62
5.5.360.0030.04217.53
5.5.350.0030.04017.54
5.5.340.0000.04318.09
5.5.330.0030.03917.71
5.5.320.0030.03917.84
5.5.310.0030.07518.08
5.5.300.0070.03618.08
5.5.290.0070.03818.13
5.5.280.0030.04418.06
5.5.270.0000.04318.12
5.5.260.0000.04417.83
5.5.250.0030.04617.91
5.5.240.0000.04417.45
5.5.230.0030.03817.40
5.5.220.0030.04217.50
5.5.210.0000.04317.47
5.5.200.0030.03817.39
5.5.190.0070.06417.38
5.5.180.0170.03717.19
5.5.160.0030.03817.18
5.5.150.0000.04217.38
5.5.140.0070.03417.21
5.5.130.0000.04317.34
5.5.120.0030.03817.09
5.5.110.0030.03817.41
5.5.100.0030.04017.35
5.5.90.0030.03617.11
5.5.80.0030.04017.38
5.5.70.0060.03817.30
5.5.60.0000.04117.33
5.5.50.0040.03517.24
5.5.40.0070.04017.25
5.5.30.0170.03717.32
5.5.20.0030.03617.32
5.5.10.0030.05817.28
5.5.00.0000.04317.41
5.4.450.0030.04219.40
5.4.440.0030.04019.56
5.4.430.0070.03419.51
5.4.420.0000.04119.43
5.4.410.0070.04319.14
5.4.400.0070.03319.08
5.4.390.0030.03819.09
5.4.380.0030.05319.11
5.4.370.0000.05018.95
5.4.360.0060.05418.89
5.4.350.0130.05618.98
5.4.340.0000.04218.98
5.4.320.0070.03818.99
5.4.310.0000.04618.98
5.4.300.0070.04019.23
5.4.290.0000.04018.98
5.4.280.0030.04019.07
5.4.270.0030.04219.08
5.4.260.0000.04519.23
5.4.250.0000.03918.98
5.4.240.0000.07518.97
5.4.230.0030.04319.23
5.4.220.0060.03918.98
5.4.210.0100.02919.20
5.4.200.0030.03619.08
5.4.190.0000.04319.24
5.4.180.0060.04518.93
5.4.170.0030.06118.98
5.4.160.0030.03719.06
5.4.150.0000.04019.05
5.4.140.0030.03916.95
5.4.130.0030.03416.95
5.4.120.0000.05116.95
5.4.110.0000.03716.95
5.4.100.0030.04616.95
5.4.90.0000.04016.95
5.4.80.0060.03616.95
5.4.70.0000.04616.95
5.4.60.0000.04116.95
5.4.50.0070.03316.95
5.4.40.0000.04416.95
5.4.30.0030.04316.95
5.4.20.0170.04616.95
5.4.10.0090.03716.95
5.4.00.0000.04116.95
5.3.290.0060.03816.95
5.3.280.0030.05016.95
5.3.270.0030.06716.95
5.3.260.0000.04616.95
5.3.250.0030.03716.95
5.3.240.0030.03616.95
5.3.230.0000.03916.95
5.3.220.0030.04516.95
5.3.210.0000.06816.95
5.3.200.0070.03216.95
5.3.190.0030.03716.95
5.3.180.0000.03816.95
5.3.170.0040.03616.95
5.3.160.0070.03616.95
5.3.150.0130.04216.95
5.3.140.0060.03516.95
5.3.130.0000.04216.95
5.3.120.0370.03716.95
5.3.110.0030.04416.95
5.3.100.0000.04316.95
5.3.90.0040.03916.95
5.3.80.0000.04516.95
5.3.70.0040.03916.95
5.3.60.0070.03716.95
5.3.50.0030.03516.95
5.3.40.0100.03216.95
5.3.30.0070.03516.95
5.3.20.0030.04116.95
5.3.10.0070.03316.95
5.3.00.0030.03816.95
5.2.170.0060.02916.95
5.2.160.0030.02916.95
5.2.150.0140.03416.95
5.2.140.0000.03416.95
5.2.130.0040.02716.95
5.2.120.0000.03416.95
5.2.110.0060.02816.95
5.2.100.0000.03716.95
5.2.90.0030.03116.95
5.2.80.0070.02716.95
5.2.70.0030.03116.95
5.2.60.0000.03416.95
5.2.50.0090.02716.95
5.2.40.0090.03116.95
5.2.30.0090.02816.95
5.2.20.0090.02516.95
5.2.10.0090.02716.95
5.2.00.0100.02616.95
5.1.60.0030.02516.95
5.1.50.0040.02616.95
5.1.40.0000.03116.95
5.1.30.0060.02416.95
5.1.20.0000.03216.95
5.1.10.0070.02316.95
5.1.00.0030.02416.95
5.0.50.0000.02216.95
5.0.40.0030.02016.95
5.0.30.0030.03116.95
5.0.20.0030.01916.95
5.0.10.0090.01616.95
5.0.00.0000.03516.95
4.4.90.0030.01516.95
4.4.80.0030.01516.95
4.4.70.0000.01816.95
4.4.60.0030.01616.95
4.4.50.0060.01416.95
4.4.40.0030.02416.95
4.4.30.0040.01516.95
4.4.20.0030.01516.95
4.4.10.0030.02016.95
4.4.00.0050.02016.95
4.3.110.0060.01316.95
4.3.100.0000.02016.95
4.3.90.0030.01416.95
4.3.80.0030.02216.95
4.3.70.0050.01116.95
4.3.60.0030.01316.95
4.3.50.0030.01616.95
4.3.40.0000.02916.95
4.3.30.0000.02616.95
4.3.20.0000.01716.95
4.3.10.0030.01616.95
4.3.00.0000.01716.95

preferences:
36.87 ms | 401 KiB | 5 Q