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); var_dump($parent); $parent['children'][$key] = $sub; } var_dump($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.0090.01318.43
8.3.50.0110.00522.17
8.3.40.0110.01118.79
8.3.30.0110.00418.87
8.3.20.0030.00518.85
8.3.10.0030.00521.08
8.3.00.0040.00819.07
8.2.180.0120.00316.63
8.2.170.0120.00322.96
8.2.160.0040.01120.11
8.2.150.0030.00524.18
8.2.140.0040.00424.66
8.2.130.0040.00426.16
8.2.120.0100.00721.18
8.2.110.0050.00521.12
8.2.100.0060.00617.84
8.2.90.0080.00019.21
8.2.80.0050.00317.97
8.2.70.0060.00317.75
8.2.60.0040.00417.93
8.2.50.0080.00018.10
8.2.40.0080.00018.22
8.2.30.0040.00421.21
8.2.20.0040.00417.69
8.2.10.0000.00817.84
8.2.00.0040.00417.95
8.1.280.0000.01525.92
8.1.270.0070.00023.99
8.1.260.0090.00626.35
8.1.250.0050.00328.09
8.1.240.0060.00322.06
8.1.230.0120.00017.89
8.1.220.0090.00017.74
8.1.210.0050.00318.77
8.1.200.0060.00317.23
8.1.190.0060.00317.35
8.1.180.0040.00418.10
8.1.170.0040.00418.59
8.1.160.0000.00718.88
8.1.150.0060.00318.68
8.1.140.0040.00417.46
8.1.130.0000.00717.86
8.1.120.0000.00717.57
8.1.110.0000.00717.51
8.1.100.0050.00317.55
8.1.90.0040.00417.38
8.1.80.0000.00717.48
8.1.70.0000.00717.46
8.1.60.0040.00417.64
8.1.50.0070.00017.57
8.1.40.0000.00917.55
8.1.30.0040.00417.71
8.1.20.0000.00917.64
8.1.10.0000.01017.57
8.1.00.0040.00417.36
8.0.300.0000.00718.77
8.0.290.0050.00716.75
8.0.280.0030.00318.46
8.0.270.0070.00017.29
8.0.260.0000.00616.90
8.0.250.0040.00416.93
8.0.240.0000.00816.92
8.0.230.0000.00716.95
8.0.220.0000.00717.03
8.0.210.0030.00416.89
8.0.200.0000.00716.89
8.0.190.0080.00016.96
8.0.180.0000.00917.05
8.0.170.0080.00016.96
8.0.160.0000.00817.07
8.0.150.0000.00716.82
8.0.140.0000.00816.97
8.0.130.0030.00313.38
8.0.120.0000.00816.87
8.0.110.0000.00817.00
8.0.100.0040.00416.76
8.0.90.0050.00316.88
8.0.80.0160.00316.93
8.0.70.0040.00416.83
8.0.60.0000.00717.02
8.0.50.0000.00717.02
8.0.30.0130.01417.10
8.0.20.0080.01217.40
8.0.10.0000.00717.05
8.0.00.0120.00916.86
7.4.330.0020.00216.73
7.4.320.0000.00616.66
7.4.300.0000.00616.63
7.4.290.0050.00316.64
7.4.280.0040.00416.69
7.4.270.0000.00716.56
7.4.260.0000.00513.35
7.4.250.0030.00716.39
7.4.240.0040.00416.52
7.4.230.0040.00416.54
7.4.220.0030.01716.51
7.4.210.0090.00716.58
7.4.200.0000.00716.54
7.4.190.0030.00516.68
7.4.160.0050.01516.53
7.4.150.0180.00617.40
7.4.140.0130.01017.86
7.4.130.0140.00516.51
7.4.120.0070.01316.58
7.4.110.0150.00316.58
7.4.100.0140.00516.51
7.4.90.0130.00616.68
7.4.80.0110.00619.39
7.4.70.0110.00916.50
7.4.60.0080.00916.65
7.4.50.0080.00016.64
7.4.40.0040.01522.77
7.4.30.0030.01516.56
7.4.00.0060.01115.07
7.3.330.0000.00613.45
7.3.320.0030.00313.41
7.3.310.0070.00016.29
7.3.300.0040.00416.45
7.3.290.0080.00616.48
7.3.280.0060.01116.43
7.3.270.0190.00017.40
7.3.260.0180.00818.24
7.3.250.0110.01116.63
7.3.240.0050.01316.33
7.3.230.0130.00716.43
7.3.210.0070.01016.68
7.3.200.0210.00419.39
7.3.190.0110.00716.50
7.3.180.0090.00916.60
7.3.170.0090.00816.61
7.3.160.0100.01316.37
7.3.120.0060.01214.86
7.3.110.0020.01414.57
7.3.100.0050.01214.89
7.3.90.0080.00514.89
7.3.80.0070.00714.86
7.3.70.0050.00814.95
7.3.60.0060.00714.98
7.3.50.0070.00614.89
7.3.40.0090.00314.95
7.3.30.0040.01114.81
7.3.20.0050.01016.70
7.3.10.0110.00616.71
7.3.00.0070.00716.65
7.2.330.0110.00916.95
7.2.320.0070.01016.71
7.2.310.0040.01616.50
7.2.300.0140.00416.70
7.2.290.0090.00916.87
7.2.250.0040.01515.11
7.2.240.0120.00815.01
7.2.230.0040.01615.22
7.2.220.0060.00715.14
7.2.210.0070.00515.23
7.2.200.0020.01315.20
7.2.190.0030.01015.07
7.2.180.0090.00414.99
7.2.170.0070.00815.08
7.2.60.0070.00316.92
7.2.00.0040.00819.41
7.1.330.0050.00815.83
7.1.320.0040.00715.83
7.1.310.0100.00315.70
7.1.300.0020.01015.75
7.1.290.0090.00315.97
7.1.280.0090.00615.72
7.1.270.0060.00715.90
7.1.260.0070.00615.71
7.1.200.0030.00815.67
7.1.100.0040.00718.07
7.1.70.0130.00317.19
7.1.60.0030.00917.05
7.1.50.0150.00616.99
7.1.00.0030.03722.32
7.0.200.0000.01216.78
7.0.140.0070.07022.08
7.0.100.0100.03720.04
7.0.90.0100.04320.02
7.0.80.0070.03719.89
7.0.70.0100.07720.08
7.0.60.0100.03719.94
7.0.50.0000.05720.27
7.0.40.0030.04020.13
7.0.30.0000.08720.03
7.0.20.0030.06320.01
7.0.10.0030.05320.08
7.0.00.0100.07020.10
5.6.280.0070.07021.08
5.6.250.0030.04320.63
5.6.240.0070.05320.52
5.6.230.0030.04720.59
5.6.220.0100.07720.58
5.6.210.0070.07020.75
5.6.200.0070.06021.05
5.6.190.0100.03721.05
5.6.180.0030.04720.94
5.6.170.0130.07720.97
5.6.160.0070.08321.06
5.6.150.0030.06321.02
5.6.140.0100.05321.14
5.6.130.0130.07021.09
5.6.120.0100.03321.04
5.6.110.0030.04021.01
5.6.100.0030.07021.02
5.6.90.0000.04721.02
5.6.80.0070.07720.33
5.6.70.0130.07320.32
5.6.60.0070.04020.42
5.6.50.0100.05720.43
5.6.40.0100.05020.41
5.6.30.0130.03020.41
5.6.20.0070.04720.40
5.6.10.0100.03720.43
5.6.00.0070.07720.46
5.5.380.0070.04020.40
5.5.370.0070.03720.38
5.5.360.0070.05320.45
5.5.350.0100.08020.38
5.5.340.0030.04320.65
5.5.330.0030.05320.93
5.5.320.0070.04020.93
5.5.310.0030.05020.92
5.5.300.0070.08020.94
5.5.290.0100.06020.64
5.5.280.0130.07720.84
5.5.270.0100.04020.87
5.5.260.0070.07320.84
5.5.250.0070.07320.70
5.5.240.0130.06720.22
5.5.230.0000.04320.22
5.5.220.0070.05020.20
5.5.210.0030.06020.02
5.5.200.0030.03720.20
5.5.190.0130.07720.25
5.5.180.0070.07720.27
5.5.160.0070.04020.13
5.5.150.0100.03020.13
5.5.140.0070.06320.16
5.5.130.0000.04020.00
5.5.120.0130.05020.11
5.5.110.0030.07720.14
5.5.100.0070.06020.16
5.5.90.0100.07320.02
5.5.80.0030.08320.17
5.5.70.0030.04320.10
5.5.60.0070.07319.94
5.5.50.0070.04320.01
5.5.40.0000.04320.11
5.5.30.0030.04019.96
5.5.20.0030.05019.96
5.5.10.0070.04019.98
5.5.00.0030.03720.00
5.4.450.0070.08019.21
5.4.440.0070.06319.54
5.4.430.0100.03019.17
5.4.420.0030.03719.41
5.4.410.0030.05019.09
5.4.400.0100.06719.05
5.4.390.0070.04318.90
5.4.380.0070.05719.12
5.4.370.0070.04319.15
5.4.360.0130.05319.20
5.4.350.0030.04019.21
5.4.340.0030.08018.96
5.4.320.0070.03319.03
5.4.310.0030.05719.23
5.4.300.0100.03018.86
5.4.290.0100.05719.14
5.4.280.0130.05719.20
5.4.270.0170.05319.09
5.4.260.0070.04319.04
5.4.250.0100.07318.87
5.4.240.0130.06719.02
5.4.230.0100.07019.13
5.4.220.0030.05019.11
5.4.210.0030.05019.08
5.4.200.0070.03319.20
5.4.190.0030.03718.94
5.4.180.0000.04718.86
5.4.170.0000.04019.17
5.4.160.0030.05318.87
5.4.150.0070.03719.23
5.4.140.0100.03316.25
5.4.130.0130.06316.45
5.4.120.0000.03316.49
5.4.110.0000.03716.44
5.4.100.0000.03316.48
5.4.90.0030.03016.54
5.4.80.0070.03316.47
5.4.70.0070.03016.52
5.4.60.0070.03016.36
5.4.50.0070.03316.29
5.4.40.0070.03316.45
5.4.30.0070.03316.40
5.4.20.0030.03716.39
5.4.10.0070.03716.41
5.4.00.0030.03315.90
5.3.290.0070.03715.73
5.3.280.0070.04315.73
5.3.270.0000.04015.73
5.3.260.0000.04015.73
5.3.250.0030.03715.73
5.3.240.0000.03315.73
5.3.230.0000.03315.73
5.3.220.0030.03015.73
5.3.210.0030.03015.73
5.3.200.0070.03015.73
5.3.190.0000.03715.73
5.3.180.0130.02315.73
5.3.170.0070.07015.73
5.3.160.0030.04315.73
5.3.150.0030.03015.73
5.3.140.0030.03715.73
5.3.130.0100.03315.73
5.3.120.0070.03315.73
5.3.110.0070.03715.73
5.3.100.0030.04015.73
5.3.90.0030.03015.73
5.3.80.0000.03315.73
5.3.70.0030.03715.73
5.3.60.0170.02715.73
5.3.50.0000.03315.73
5.3.40.0030.03715.73
5.3.30.0000.03315.73
5.3.20.0000.04315.73
5.3.10.0030.03315.73
5.3.00.0030.04015.73
5.2.170.0030.03315.73
5.2.160.0070.02015.73
5.2.150.0030.02315.73
5.2.140.0100.02315.73
5.2.130.0070.02315.73
5.2.120.0000.03715.73
5.2.110.0000.03015.73
5.2.100.0000.02715.73
5.2.90.0000.03015.73
5.2.80.0000.03015.73
5.2.70.0000.03015.73
5.2.60.0030.02315.73
5.2.50.0000.03015.73
5.2.40.0100.01715.73
5.2.30.0000.03015.73
5.2.20.0070.02015.73
5.2.10.0070.02315.73
5.2.00.0070.02015.73
5.1.60.0030.02015.73
5.1.50.0000.02015.73
5.1.40.0030.02015.73
5.1.30.0000.02715.73
5.1.20.0100.02015.73
5.1.10.0030.02015.73
5.1.00.0030.02315.73
5.0.50.0000.05015.73
5.0.40.0030.02715.73
5.0.30.0030.06315.73
5.0.20.0000.04715.73
5.0.10.0030.04715.73
5.0.00.0000.06015.73
4.4.90.0000.01715.73
4.4.80.0030.01315.73
4.4.70.0000.01315.73
4.4.60.0000.01315.73
4.4.50.0000.01315.73
4.4.40.0030.02015.73
4.4.30.0030.01015.73
4.4.20.0000.01315.73
4.4.10.0030.01015.73
4.4.00.0030.05315.73
4.3.110.0000.03715.73
4.3.100.0030.01015.73
4.3.90.0070.02315.73
4.3.80.0070.02715.73
4.3.70.0000.02015.73
4.3.60.0000.03715.73
4.3.50.0030.01315.73
4.3.40.0000.04715.73
4.3.30.0030.02715.73
4.3.20.0030.01315.73
4.3.10.0030.03315.73
4.3.00.0000.04015.73

preferences:
41.01 ms | 401 KiB | 5 Q