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 FALSE; } function v2($array) { $out = array(); foreach ($array as $key => $sub) { if(!isset($sub['parent_id'])) { $out[$key] = $sub; } else { 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.0120.00318.31
8.3.50.0170.00722.00
8.3.40.0090.00918.84
8.3.30.0100.00319.22
8.3.20.0040.00420.20
8.3.10.0080.00023.52
8.3.00.0060.00320.84
8.2.180.0110.00718.16
8.2.170.0040.01222.96
8.2.160.0070.01020.35
8.2.150.0080.00024.18
8.2.140.0050.00324.66
8.2.130.0040.00426.16
8.2.120.0050.00320.86
8.2.110.0100.00022.30
8.2.100.0120.00017.91
8.2.90.0000.00819.35
8.2.80.0000.00817.97
8.2.70.0070.00417.63
8.2.60.0000.00817.92
8.2.50.0040.00418.07
8.2.40.0030.00520.07
8.2.30.0100.00018.35
8.2.20.0040.00417.66
8.2.10.0000.00718.04
8.2.00.0040.00417.84
8.1.280.0140.00725.92
8.1.270.0050.00322.18
8.1.260.0040.00426.35
8.1.250.0040.00428.09
8.1.240.0090.00023.95
8.1.230.0100.00318.96
8.1.220.0080.00317.74
8.1.210.0060.00318.77
8.1.200.0030.00717.35
8.1.190.0030.00517.36
8.1.180.0050.00318.10
8.1.170.0040.00418.59
8.1.160.0080.00022.05
8.1.150.0000.00718.68
8.1.140.0040.00417.49
8.1.130.0000.00717.83
8.1.120.0040.00417.55
8.1.110.0030.00717.54
8.1.100.0040.00417.41
8.1.90.0000.00717.49
8.1.80.0060.00317.38
8.1.70.0050.00317.38
8.1.60.0040.00417.64
8.1.50.0000.00917.56
8.1.40.0040.00417.54
8.1.30.0030.00617.68
8.1.20.0040.00417.69
8.1.10.0040.00417.52
8.1.00.0040.00417.49
8.0.300.0040.00418.77
8.0.290.0070.00016.88
8.0.280.0050.00218.54
8.0.270.0000.00917.34
8.0.260.0030.00317.33
8.0.250.0000.00716.98
8.0.240.0000.00717.02
8.0.230.0030.00317.03
8.0.220.0030.00316.93
8.0.210.0030.00317.02
8.0.200.0000.00717.04
8.0.190.0040.00417.01
8.0.180.0000.00716.96
8.0.170.0050.00317.02
8.0.160.0050.00316.84
8.0.150.0040.00416.78
8.0.140.0040.00416.82
8.0.130.0050.00213.34
8.0.120.0030.00616.88
8.0.110.0030.00616.91
8.0.100.0040.00416.89
8.0.90.0050.00216.83
8.0.80.0110.00516.92
8.0.70.0000.00816.87
8.0.60.0040.00416.95
8.0.50.0040.00416.93
8.0.30.0120.00617.21
8.0.20.0090.01317.40
8.0.10.0000.00816.92
8.0.00.0100.01216.77
7.4.330.0030.00315.07
7.4.320.0000.00616.55
7.4.300.0040.00416.64
7.4.290.0030.00516.60
7.4.280.0060.00316.44
7.4.270.0000.00816.63
7.4.260.0030.00516.59
7.4.250.0040.00416.60
7.4.240.0070.00016.55
7.4.230.0040.00416.66
7.4.220.0120.00616.46
7.4.210.0100.01016.67
7.4.200.0070.00016.73
7.4.160.0060.01816.42
7.4.150.0140.00417.40
7.4.140.0060.01217.86
7.4.130.0080.01116.57
7.4.120.0090.00916.54
7.4.110.0080.01116.60
7.4.100.0120.00616.67
7.4.90.0050.01116.54
7.4.80.0120.00619.39
7.4.70.0060.01216.53
7.4.60.0170.00016.49
7.4.50.0040.00416.71
7.4.40.0110.00616.55
7.4.30.0080.00816.51
7.4.10.0040.01214.84
7.4.00.0090.00714.84
7.3.330.0080.00013.39
7.3.320.0050.00013.34
7.3.310.0030.00316.25
7.3.300.0040.00316.20
7.3.290.0110.00716.34
7.3.280.0060.01216.36
7.3.270.0160.00317.40
7.3.260.0040.01316.70
7.3.250.0120.01016.49
7.3.240.0090.01216.63
7.3.230.0090.01416.44
7.3.210.0140.00316.38
7.3.200.0090.00919.39
7.3.190.0100.00716.67
7.3.180.0060.01016.41
7.3.170.0080.00816.57
7.3.160.0060.01016.39
7.3.130.0030.01414.95
7.3.120.0060.00914.78
7.3.110.0050.01114.90
7.3.100.0080.00814.86
7.3.90.0050.00914.94
7.3.80.0060.00714.85
7.3.70.0020.01214.88
7.3.60.0020.01314.83
7.3.50.0070.01014.72
7.3.40.0030.01014.91
7.3.30.0050.00814.72
7.3.20.0080.00316.68
7.3.10.0100.00416.57
7.3.00.0080.00316.69
7.2.330.0070.01316.67
7.2.320.0040.01516.90
7.2.310.0170.00016.80
7.2.300.0060.01216.73
7.2.290.0090.00916.69
7.2.260.0090.00915.16
7.2.250.0100.00714.90
7.2.240.0040.01514.91
7.2.230.0050.01215.26
7.2.220.0030.01415.04
7.2.210.0060.00715.00
7.2.200.0030.01015.06
7.2.190.0070.00714.98
7.2.180.0100.00415.03
7.2.170.0060.00615.04
7.2.160.0070.01015.16
7.2.150.0140.00016.98
7.2.140.0000.01617.00
7.2.130.0080.00817.11
7.2.120.0000.01517.05
7.2.110.0060.01216.96
7.2.100.0080.00816.73
7.2.90.0080.00817.04
7.2.80.0110.00716.89
7.2.70.0060.00616.99
7.2.60.0070.01116.88
7.2.50.0080.00816.85
7.2.40.0030.01016.94
7.2.30.0060.00917.02
7.2.20.0100.00316.79
7.2.10.0060.00616.79
7.2.00.0070.00618.20
7.1.330.0060.00915.82
7.1.320.0070.00815.48
7.1.310.0100.00515.81
7.1.300.0020.01315.68
7.1.290.0050.01115.71
7.1.280.0050.00615.90
7.1.270.0050.00715.71
7.1.260.0020.01215.89
7.1.250.0130.00315.78
7.1.240.0090.00915.82
7.1.230.0070.01015.41
7.1.220.0030.01215.57
7.1.210.0040.01115.73
7.1.200.0060.00915.86
7.1.190.0110.00015.97
7.1.180.0040.00715.73
7.1.170.0060.00615.65
7.1.160.0070.00715.82
7.1.150.0000.01215.51
7.1.140.0030.01215.80
7.1.130.0070.01015.87
7.1.120.0100.00315.71
7.1.110.0100.00315.73
7.1.100.0080.00516.82
7.1.90.0000.01315.58
7.1.80.0090.00015.55
7.1.70.0020.01216.30
7.1.60.0020.01817.69
7.1.50.0050.01416.44
7.1.40.0030.01215.86
7.1.30.0120.00015.52
7.1.20.0030.01015.61
7.1.10.0000.01015.81
7.1.00.0050.04119.13
7.0.330.0000.00915.35
7.0.320.0040.00815.50
7.0.310.0090.00915.04
7.0.300.0030.01215.41
7.0.290.0090.00615.54
7.0.280.0030.00715.39
7.0.270.0060.01015.28
7.0.260.0030.01015.48
7.0.250.0040.00415.10
7.0.240.0040.01115.18
7.0.230.0040.00415.17
7.0.220.0040.00715.50
7.0.210.0060.00615.43
7.0.200.1970.00515.96
7.0.190.0000.00815.38
7.0.180.0070.00715.52
7.0.170.0030.01015.26
7.0.160.0000.01315.48
7.0.150.0040.01115.15
7.0.140.0000.04518.81
7.0.130.0060.00315.61
7.0.120.0090.00315.39
7.0.110.0100.00315.49
7.0.100.0050.04317.75
7.0.90.0100.03817.69
7.0.80.0100.03717.65
7.0.70.0070.02217.52
7.0.60.0070.02017.79
7.0.50.0180.02518.00
7.0.40.0030.04716.74
7.0.30.0030.03016.66
7.0.20.0080.04516.85
7.0.10.0110.02516.77
7.0.00.0070.02916.78
5.6.400.0030.01114.46
5.6.390.0060.00914.46
5.6.380.0060.01214.13
5.6.370.0060.00914.61
5.6.360.0040.00714.06
5.6.350.0060.00914.25
5.6.340.0100.00014.29
5.6.330.0080.00314.73
5.6.320.0000.01214.14
5.6.310.0100.00314.30
5.6.300.0130.00014.17
5.6.290.0030.00914.53
5.6.280.0050.04017.58
5.6.270.0060.00614.70
5.6.260.0090.00614.51
5.6.250.0050.03817.50
5.6.240.0120.02517.53
5.6.230.0030.04617.42
5.6.220.0060.02617.53
5.6.210.0020.04717.39
5.6.200.0060.04617.86
5.6.190.0050.04817.70
5.6.180.0050.04517.68
5.6.170.0080.02717.65
5.6.160.0050.03917.71
5.6.150.0080.04517.66
5.6.140.0080.04417.72
5.6.130.0070.04517.83
5.6.120.0060.04317.64
5.6.110.0080.04117.61
5.6.100.0050.04917.73
5.6.90.0050.02317.72
5.6.80.0120.01917.47
5.6.70.0050.02317.26
5.6.60.0060.02317.33
5.6.50.0070.02217.52
5.6.40.0030.02317.34
5.6.30.0080.02517.44
5.6.20.0050.01917.36
5.6.10.0020.02217.44
5.6.00.0090.01817.33
5.5.380.0080.03517.33
5.5.370.0060.04417.50
5.5.360.0080.04017.37
5.5.350.0030.04517.40
5.5.340.0050.03817.70
5.5.330.0120.03517.61
5.5.320.0050.04817.48
5.5.310.0070.02817.54
5.5.300.0100.04217.39
5.5.290.0060.04517.63
5.5.280.0050.03117.46
5.5.270.0120.04017.44
5.5.260.0040.04517.57
5.5.250.0090.04417.49
5.5.240.0140.02617.32
5.5.230.0080.02517.30
5.5.220.0060.02317.26
5.5.210.0130.02117.25
5.5.200.0050.02517.17
5.5.190.0060.02117.05
5.5.180.0060.02317.21
5.5.170.0060.00914.00
5.5.160.0120.01717.03
5.5.150.0070.02317.35
5.5.140.0080.01817.25
5.5.130.0040.02217.02
5.5.120.0060.02417.21
5.5.110.0040.02217.35
5.5.100.0020.02117.23
5.5.90.0030.02017.12
5.5.80.0060.01817.14
5.5.70.0050.02517.25
5.5.60.0050.02117.16
5.5.50.0120.01717.09
5.5.40.0070.01817.22
5.5.30.0050.01917.26
5.5.20.0070.02017.32
5.5.10.0080.01817.20
5.5.00.0030.02217.20
5.4.450.0090.03815.39
5.4.440.0080.04415.34
5.4.430.0060.04115.23
5.4.420.0090.03915.23
5.4.410.0080.03115.22
5.4.400.0040.02315.06
5.4.390.0000.02615.05
5.4.380.0060.02015.12
5.4.370.0050.02115.21
5.4.360.0070.01915.10
5.4.350.0040.02115.13
5.4.340.0020.02715.20
5.4.330.0050.01011.26
5.4.320.0050.01815.09
5.4.310.0060.01615.15
5.4.300.0020.02015.08
5.4.290.0040.01815.24
5.4.280.0020.02515.17
5.4.270.0030.01914.99
5.4.260.0050.02015.02
5.4.250.0000.02315.18
5.4.240.0030.02415.03
5.4.230.0100.01714.89
5.4.220.0050.01715.09
5.4.210.0020.01814.96
5.4.200.0020.01815.11
5.4.190.0040.02015.09
5.4.180.0070.02014.94
5.4.170.0010.02014.99
5.4.160.0020.02114.85
5.4.150.0050.01715.00
5.4.140.0000.02213.80
5.4.130.0050.01813.65
5.4.120.0040.02013.95
5.4.110.0050.01713.70
5.4.100.0020.02113.84
5.4.90.0030.02113.86
5.4.80.0020.02113.70
5.4.70.0030.01913.83
5.4.60.0030.01913.89
5.4.50.0050.01813.71
5.4.40.0080.01413.87
5.4.30.0050.02713.76
5.4.20.0050.01813.83
5.4.10.0020.02013.84
5.4.00.0020.02313.56
5.3.290.0020.02412.72
5.3.280.0030.02112.60
5.3.270.0020.02212.75
5.3.260.0060.01812.70
5.3.250.0080.01512.71
5.3.240.0000.02512.58
5.3.230.0020.02112.50
5.3.220.0050.02412.67
5.3.210.0000.02212.50
5.3.200.0020.01912.57
5.3.190.0040.01712.61
5.3.180.0090.01512.69
5.3.170.0060.01712.68
5.3.160.0030.01612.67
5.3.150.0050.01712.63
5.3.140.0050.01812.59
5.3.130.0020.02012.57
5.3.120.0060.01612.58
5.3.110.0060.01712.67
5.3.100.0040.02912.40
5.3.90.0070.02112.38
5.3.80.0040.04312.26
5.3.70.0060.02912.34
5.3.60.0020.03412.41
5.3.50.0100.02512.36
5.3.40.0050.03712.25
5.3.30.0050.02312.20
5.3.20.0040.04012.13
5.3.10.0100.02911.99
5.3.00.0030.04212.01
5.2.170.0030.06711.06
5.2.160.0070.04011.02
5.2.150.0030.05311.18
5.2.140.0130.05711.21
5.2.130.0070.06311.18
5.2.120.0100.06011.11
5.2.110.0130.05311.01
5.2.100.0000.05711.14
5.2.90.0070.04311.17
5.2.80.0070.06011.23
5.2.70.0000.04711.02
5.2.60.0000.03711.07
5.2.50.0130.03311.13
5.2.40.0030.06011.02
5.2.30.0070.05311.08
5.2.20.0070.04310.90
5.2.10.0030.02710.86
5.2.00.0070.06010.84
5.1.60.0070.03010.00
5.1.50.0100.04310.00
5.1.40.0000.0539.84
5.1.30.0070.05310.25
5.1.20.0030.04310.30
5.1.10.0030.0579.92
5.1.00.0070.0239.92
5.0.50.0030.0279.21
5.0.40.0000.0279.21
5.0.30.0000.0639.21
5.0.20.0030.0439.21
5.0.10.0030.0379.21
5.0.00.0030.0609.21
4.4.90.0000.0279.21
4.4.80.0000.0309.21
4.4.70.0000.0339.21
4.4.60.0000.0279.21
4.4.50.0070.0309.21
4.4.40.0030.0409.21
4.4.30.0000.0179.21
4.4.20.0030.0209.21
4.4.10.0000.0339.21
4.4.00.0030.0239.21
4.3.110.0000.0279.21
4.3.100.0070.0209.21
4.3.90.0030.0379.21
4.3.80.0070.0479.21
4.3.70.0030.0309.21
4.3.60.0070.0209.21
4.3.50.0070.0239.21
4.3.40.0000.0309.21
4.3.30.0030.0339.21
4.3.20.0030.0279.21
4.3.10.0000.0339.21
4.3.00.0030.0179.21

preferences:
53.73 ms | 400 KiB | 5 Q