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($array, $key) { if(array_key_exists($key, $array)) { return $array[$key]; } else { foreach($array as $sub) { if(FALSE !== ($found = find_by_key($sub, $key))) { 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($out, $sub['parent_id']); $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.70.0070.01118.80
8.3.60.0170.00318.77
8.3.50.0110.00721.27
8.3.40.0150.00719.06
8.3.30.0070.01018.96
8.3.20.0000.00818.95
8.3.10.0040.00421.11
8.3.00.0090.00020.61
8.2.180.0110.00716.87
8.2.170.0070.00722.96
8.2.160.0070.00720.35
8.2.150.0040.00424.18
8.2.140.0000.00824.66
8.2.130.0070.00026.16
8.2.120.0060.00321.00
8.2.110.0030.00620.39
8.2.100.0080.00417.85
8.2.90.0000.00818.16
8.2.80.0020.00519.47
8.2.70.0090.00017.63
8.2.60.0030.00618.05
8.2.50.0060.00318.07
8.2.40.0040.00418.03
8.2.30.0080.00021.28
8.2.20.0060.00317.95
8.2.10.0050.00317.82
8.2.00.0030.00617.87
8.1.280.0090.00625.92
8.1.270.0000.00823.99
8.1.260.0050.00326.35
8.1.250.0000.00728.09
8.1.240.0060.00322.05
8.1.230.0040.00820.77
8.1.220.0000.00917.74
8.1.210.0090.00018.85
8.1.200.0070.00317.35
8.1.190.0030.00617.51
8.1.180.0060.00318.10
8.1.170.0060.00318.46
8.1.160.0040.00419.00
8.1.150.0080.00018.75
8.1.140.0050.00317.35
8.1.130.0030.00317.88
8.1.120.0070.00017.53
8.1.110.0000.00817.40
8.1.100.0040.00417.39
8.1.90.0040.00417.42
8.1.80.0000.00717.50
8.1.70.0030.00317.44
8.1.60.0040.00417.62
8.1.50.0040.00417.44
8.1.40.0050.00317.61
8.1.30.0080.00017.65
8.1.20.0040.00417.55
8.1.10.0080.00017.64
8.1.00.0000.00817.56
8.0.300.0040.00420.25
8.0.290.0070.00017.30
8.0.280.0070.00018.50
8.0.270.0000.00717.27
8.0.260.0070.00016.93
8.0.250.0040.00417.07
8.0.240.0060.00317.09
8.0.230.0070.00016.94
8.0.220.0000.00717.07
8.0.210.0040.00417.02
8.0.200.0030.00316.94
8.0.190.0000.00916.93
8.0.180.0000.00717.07
8.0.170.0040.00416.91
8.0.160.0000.00817.09
8.0.150.0040.00416.91
8.0.140.0030.00516.86
8.0.130.0060.00013.43
8.0.120.0040.00416.98
8.0.110.0040.00417.04
8.0.100.0070.00016.86
8.0.90.0000.00817.07
8.0.80.0040.01117.00
8.0.70.0020.00517.07
8.0.60.0040.00416.95
8.0.50.0050.00316.83
8.0.30.0080.01317.30
8.0.20.0080.01117.40
8.0.10.0050.00216.94
8.0.00.0120.00816.97
7.4.330.0050.00016.71
7.4.320.0070.00016.68
7.4.300.0000.00716.61
7.4.290.0000.00716.55
7.4.280.0040.00416.47
7.4.270.0070.00016.58
7.4.260.0050.00013.28
7.4.250.0050.00316.55
7.4.240.0000.00716.60
7.4.230.0040.00416.71
7.4.220.0170.00616.50
7.4.210.0120.00516.53
7.4.200.0070.00016.46
7.4.190.0040.00416.61
7.4.160.0130.00316.49
7.4.150.0090.00917.40
7.4.140.0100.00917.86
7.4.130.0130.00616.62
7.4.120.0100.01016.55
7.4.110.0030.01316.37
7.4.100.0060.01516.57
7.4.90.0070.01116.45
7.4.80.0030.01319.39
7.4.70.0120.01216.43
7.4.60.0120.00816.74
7.4.50.0090.00016.27
7.4.40.0070.01122.77
7.4.30.0030.01916.67
7.4.10.0060.01014.70
7.4.00.0070.00915.02
7.3.330.0030.00313.22
7.3.320.0040.00413.38
7.3.310.0070.00016.46
7.3.300.0030.00316.24
7.3.290.0130.00316.40
7.3.280.0070.00816.48
7.3.270.0040.01217.40
7.3.260.0070.01218.24
7.3.250.0140.00316.50
7.3.240.0060.01116.41
7.3.230.0100.01316.41
7.3.210.0060.01516.61
7.3.200.0160.00619.39
7.3.190.0090.00916.64
7.3.180.0030.01216.42
7.3.170.0060.00916.45
7.3.160.0070.01016.40
7.3.130.0110.00714.81
7.3.120.0100.00914.79
7.3.110.0070.01114.96
7.3.100.0050.01314.82
7.3.90.0090.00514.89
7.3.80.0040.00914.70
7.3.70.0080.00714.90
7.3.60.0120.00514.82
7.3.50.0080.00414.67
7.3.40.0070.00914.96
7.3.30.0070.00714.95
7.3.20.0020.01216.72
7.3.10.0090.00516.72
7.3.00.0070.00716.69
7.2.330.0110.01116.53
7.2.320.0090.00916.55
7.2.310.0100.00716.78
7.2.300.0130.01016.85
7.2.290.0000.01616.78
7.2.260.0110.00615.33
7.2.250.0100.00714.82
7.2.240.0090.00615.07
7.2.230.0030.01015.01
7.2.220.0050.00915.06
7.2.210.0030.01115.22
7.2.200.0060.01015.10
7.2.190.0110.00514.85
7.2.180.0090.00415.02
7.2.170.0070.00915.09
7.2.160.0000.01315.16
7.2.150.0100.00716.75
7.2.140.0090.00617.16
7.2.130.0070.00317.04
7.2.120.0140.00417.02
7.2.110.0100.00716.89
7.2.100.0090.00616.80
7.2.90.0090.00916.95
7.2.80.0060.00916.87
7.2.70.0040.00817.03
7.2.60.0060.00816.73
7.2.50.0060.00316.62
7.2.40.0030.01117.00
7.2.30.0000.00917.00
7.2.20.0030.01016.70
7.2.10.0070.00417.18
7.2.00.0090.00618.22
7.1.330.0070.00915.68
7.1.320.0040.01115.79
7.1.310.0020.01015.82
7.1.300.0060.00715.78
7.1.290.0020.01115.77
7.1.280.0070.00715.73
7.1.270.0070.00815.74
7.1.260.0060.00815.80
7.1.250.0100.00715.77
7.1.240.0030.00915.85
7.1.230.0000.01315.63
7.1.220.0070.00315.94
7.1.210.0000.01315.80
7.1.200.0050.01015.86
7.1.190.0040.00815.69
7.1.180.0070.00315.77
7.1.170.0070.01015.75
7.1.160.0030.01015.61
7.1.150.0000.01315.98
7.1.140.0100.00315.80
7.1.130.0040.01115.83
7.1.120.0040.01115.84
7.1.110.0000.01515.93
7.1.100.0030.01315.99
7.1.90.0090.00316.06
7.1.80.0100.00315.84
7.1.70.0040.00516.53
7.1.60.0080.00316.48
7.1.50.0060.00816.29
7.1.40.0090.00316.37
7.1.30.0060.00816.27
7.1.20.0050.00716.36
7.1.10.0070.00516.34
7.1.00.0090.00416.29
7.0.330.0040.01115.44
7.0.320.0000.00915.32
7.0.310.0080.00415.39
7.0.300.0080.00615.20
7.0.290.0030.00615.32
7.0.280.0030.01015.27
7.0.270.0070.00715.17
7.0.260.0030.01015.25
7.0.250.0070.00415.45
7.0.240.0030.01015.65
7.0.230.0030.01015.52
7.0.220.0030.00915.18
7.0.210.0070.00715.43
7.0.200.0020.00916.08
7.0.190.0010.00816.03
7.0.180.0050.00515.90
7.0.170.0050.00515.96
7.0.160.0060.00415.85
7.0.150.0020.00815.98
7.0.140.0000.00815.86
7.0.130.0080.00316.03
7.0.120.0000.01215.93
7.0.110.0020.00615.89
7.0.100.0020.00615.90
7.0.90.0060.00515.86
7.0.80.0060.00415.84
7.0.70.0000.01215.91
7.0.60.0030.00715.66
7.0.50.0050.00716.10
7.0.40.0060.00614.25
7.0.30.0030.00814.14
7.0.20.0070.00714.23
7.0.10.0060.00614.21
7.0.00.0040.00714.10
5.6.400.0030.00914.51
5.6.390.0060.00914.43
5.6.380.0060.00614.12
5.6.370.0140.00413.98
5.6.360.0060.00914.32
5.6.350.0080.00314.63
5.6.340.0040.01114.25
5.6.330.0050.00514.06
5.6.320.0060.00614.49
5.6.310.0120.00314.30
5.6.300.0050.02417.49
5.6.290.0070.02417.54
5.6.280.0070.02617.63
5.6.270.0050.03717.57
5.6.260.0020.03017.71
5.6.250.0100.02017.63
5.6.240.0080.02417.82
5.6.230.0090.02617.70
5.6.220.0050.02717.58
5.6.210.0100.02417.43
5.6.200.0070.02517.72
5.6.190.0050.02617.78
5.6.180.0010.02817.75
5.6.170.0070.02917.57
5.6.160.0090.02517.54
5.6.150.0030.02917.79
5.6.140.0070.02417.81
5.6.130.0070.02317.68
5.6.120.0050.04017.49
5.6.110.0100.02217.66
5.6.100.0050.02417.65
5.6.90.0050.03717.53
5.6.80.0070.02817.47
5.6.70.0060.02217.26
5.6.60.0040.02517.34
5.6.50.0060.03217.35
5.6.40.0070.02517.31
5.6.30.0060.02617.27
5.6.20.0070.04117.41
5.6.10.0080.02317.34
5.6.00.0050.03517.24
5.5.380.0150.02215.85
5.5.370.0090.02416.10
5.5.360.0070.02915.84
5.5.350.0020.03015.82
5.5.340.0040.02915.90
5.5.330.0050.02516.38
5.5.320.0080.02216.11
5.5.310.0030.03016.23
5.5.300.0080.02216.41
5.5.290.0030.03016.33
5.5.280.0080.02216.28
5.5.270.0110.02116.13
5.5.260.0060.02516.35
5.5.250.0050.02816.06
5.5.240.0090.02015.85
5.5.230.0050.02215.73
5.5.220.0090.02115.72
5.5.210.0050.02415.84
5.5.200.0050.02715.65
5.5.190.0030.03115.63
5.5.180.0120.02315.94
5.5.170.0090.00614.33
5.5.160.0100.03315.99
5.5.150.0090.02215.90
5.5.140.0050.02715.89
5.5.130.0010.02715.84
5.5.120.0020.02915.71
5.5.110.0100.02515.87
5.5.100.0050.02215.76
5.5.90.0050.02215.92
5.5.80.0070.02315.78
5.5.70.0080.01915.71
5.5.60.0050.02415.68
5.5.50.0050.04615.92
5.5.40.0050.02315.84
5.5.30.0070.02315.86
5.5.20.0050.02115.86
5.5.10.0020.02615.69
5.5.00.0050.02415.71
5.4.450.0030.02316.06
5.4.440.0040.02815.95
5.4.430.0040.03415.99
5.4.420.0020.03015.95
5.4.410.0020.03115.86
5.4.400.0050.02115.83
5.4.390.0060.02315.90
5.4.380.0050.04415.85
5.4.370.0070.02715.90
5.4.360.0070.02715.83
5.4.350.0060.03715.89
5.4.340.0060.02115.86
5.4.330.0000.00812.58
5.4.320.0050.02315.84
5.4.310.0090.02315.83
5.4.300.0070.02215.74
5.4.290.0050.02615.79
5.4.280.0060.02215.83
5.4.270.0110.01815.88
5.4.260.0070.02215.73
5.4.250.0130.02015.83
5.4.240.0060.03215.83
5.4.230.0060.02215.86
5.4.220.0000.03515.82
5.4.210.0030.02515.75
5.4.200.0070.02115.87
5.4.190.0080.02515.78
5.4.180.0050.02015.78
5.4.170.0020.02815.82
5.4.160.0080.01615.88
5.4.150.0080.02015.81
5.4.140.0220.01514.55
5.4.130.0080.01914.54
5.4.120.0110.02114.65
5.4.110.0030.02714.53
5.4.100.0030.02314.69
5.4.90.0030.02814.72
5.4.80.0050.02814.55
5.4.70.0080.01614.70
5.4.60.0000.02714.67
5.4.50.0030.02914.51
5.4.40.0050.02114.60
5.4.30.0120.02914.63
5.4.20.0070.02214.59
5.4.10.0090.01914.63
5.4.00.0080.01914.29
5.3.290.0050.03613.78
5.3.280.0030.02313.74
5.3.270.0090.02113.74
5.3.260.0070.04313.73
5.3.250.0030.02313.74
5.3.240.0080.02013.73
5.3.230.0020.02813.73
5.3.220.0020.02213.73
5.3.210.0030.02213.73
5.3.200.0060.02213.73
5.3.190.0060.02113.73
5.3.180.0000.03713.73
5.3.170.0060.02013.73
5.3.160.0020.02413.73
5.3.150.0030.02513.73
5.3.140.0110.02413.73
5.3.130.0050.02313.73
5.3.120.0050.02413.73
5.3.110.0030.02513.73
5.3.100.0050.03413.73
5.3.90.0070.02113.73
5.3.80.0050.02613.73
5.3.70.0020.02613.73
5.3.60.0080.02113.73
5.3.50.0080.02713.73
5.3.40.0040.02013.73
5.3.30.0040.02413.73
5.3.20.0070.02113.73
5.3.10.0070.01713.73
5.3.00.0050.01913.73
5.2.170.0040.02814.88
5.2.160.0030.04614.88
5.2.150.0030.02814.88
5.2.140.0240.03114.88
5.2.130.0000.03114.88
5.2.120.0030.03414.88
5.2.110.0100.03014.88
5.2.100.0030.05014.88
5.2.90.0030.03414.88
5.2.80.0030.03314.88
5.2.70.0100.02314.88
5.2.60.0060.02614.88
5.2.50.0070.02614.88
5.2.40.0030.03014.88
5.2.30.0000.03414.88
5.2.20.0040.02914.88
5.2.10.0000.03714.88
5.2.00.0000.07214.88
5.1.60.0030.02414.88
5.1.50.0030.02614.88
5.1.40.0070.02414.88
5.1.30.0030.02914.88
5.1.20.0070.03314.88
5.1.10.0100.02014.88
5.1.00.0030.03014.88
5.0.50.0030.02114.88
5.0.40.0030.02514.88
5.0.30.0030.03014.88
5.0.20.0030.02014.88
5.0.10.0000.02114.88
5.0.00.0100.03314.88
4.4.90.0000.01814.88
4.4.80.0060.01314.88
4.4.70.0030.01614.88
4.4.60.0000.01914.88
4.4.50.0040.01414.88
4.4.40.0000.02914.88
4.4.30.0030.01314.88
4.4.20.0000.01914.88
4.4.10.0030.03014.88
4.4.00.0040.02514.88
4.3.110.0000.01714.88
4.3.100.0000.02214.88
4.3.90.0060.01014.88
4.3.80.0000.02914.88
4.3.70.0030.01414.88
4.3.60.0000.01714.88
4.3.50.0000.01814.88
4.3.40.0030.02214.88
4.3.30.0040.01514.88
4.3.20.0000.01714.88
4.3.10.0000.01714.88
4.3.00.0000.01814.88

preferences:
50.72 ms | 401 KiB | 5 Q