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; } 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.0140.00718.55
8.3.50.0080.01222.05
8.3.40.0150.00018.96
8.3.30.0120.00918.67
8.3.20.0040.00418.90
8.3.10.0030.00621.13
8.3.00.0070.00320.82
8.2.180.0120.00316.63
8.2.170.0120.00322.96
8.2.160.0070.00720.37
8.2.150.0080.00024.18
8.2.140.0040.00424.66
8.2.130.0080.00026.16
8.2.120.0040.00419.95
8.2.110.0070.00320.39
8.2.100.0120.00017.84
8.2.90.0040.00419.19
8.2.80.0060.00317.97
8.2.70.0000.00917.63
8.2.60.0080.00017.93
8.2.50.0040.00418.07
8.2.40.0050.00318.28
8.2.30.0040.00421.22
8.2.20.0030.00617.83
8.2.10.0040.00417.84
8.2.00.0000.00817.79
8.1.280.0150.00925.92
8.1.270.0000.00723.99
8.1.260.0070.00726.35
8.1.250.0000.00828.09
8.1.240.0000.01022.19
8.1.230.0110.00017.52
8.1.220.0030.00517.74
8.1.210.0030.00518.77
8.1.200.0060.00317.38
8.1.190.0040.00417.41
8.1.180.0000.00818.10
8.1.170.0040.00418.56
8.1.160.0000.00719.00
8.1.150.0040.00418.59
8.1.140.0060.00317.49
8.1.130.0000.00817.77
8.1.120.0000.00717.43
8.1.110.0040.00417.49
8.1.100.0000.00817.40
8.1.90.0070.00017.52
8.1.80.0030.00517.40
8.1.70.0030.00517.46
8.1.60.0060.00317.51
8.1.50.0030.00517.55
8.1.40.0040.00417.60
8.1.30.0040.00417.63
8.1.20.0030.00517.61
8.1.10.0030.00617.46
8.1.00.0000.00817.48
8.0.300.0000.00818.77
8.0.290.0000.00716.88
8.0.280.0000.00818.47
8.0.270.0000.00717.29
8.0.260.0000.00816.96
8.0.250.0040.00417.04
8.0.240.0060.00317.02
8.0.230.0030.00317.05
8.0.220.0070.00017.00
8.0.210.0020.00517.00
8.0.200.0030.00317.04
8.0.190.0000.00717.04
8.0.180.0040.00417.07
8.0.170.0070.00016.93
8.0.160.0000.00817.05
8.0.150.0070.00016.92
8.0.140.0040.00416.97
8.0.130.0000.00613.38
8.0.120.0030.00516.91
8.0.110.0000.00816.87
8.0.100.0040.00416.94
8.0.90.0040.00416.88
8.0.80.0040.01116.93
8.0.70.0060.00317.02
8.0.60.0040.00416.99
8.0.50.0040.00416.96
8.0.30.0110.01217.24
8.0.20.0160.00817.40
8.0.10.0040.00416.98
8.0.00.0140.00516.81
7.4.330.0060.00016.77
7.4.320.0030.00316.64
7.4.300.0030.00316.57
7.4.290.0070.00016.50
7.4.280.0000.00716.57
7.4.270.0000.00716.53
7.4.260.0000.00613.26
7.4.250.0030.00516.59
7.4.240.0070.00016.57
7.4.230.0030.00316.66
7.4.220.0040.01416.68
7.4.210.0100.00916.62
7.4.200.0080.00016.66
7.4.190.0030.00316.67
7.4.160.0120.00416.47
7.4.150.0110.01517.40
7.4.140.0050.01217.86
7.4.130.0110.00816.58
7.4.120.0110.00716.56
7.4.110.0070.01016.50
7.4.100.0130.00916.72
7.4.90.0060.01316.54
7.4.80.0110.01219.39
7.4.70.0080.01016.51
7.4.60.0060.01316.59
7.4.50.0060.00316.67
7.4.40.0040.01222.77
7.4.30.0090.00916.68
7.4.00.0140.00015.02
7.3.330.0030.00613.28
7.3.320.0000.00613.25
7.3.310.0070.00016.34
7.3.300.0000.00616.49
7.3.290.0070.00716.38
7.3.280.0060.01416.45
7.3.270.0170.00317.40
7.3.260.0140.01218.24
7.3.250.0110.00916.45
7.3.240.0060.01216.36
7.3.230.0140.00316.70
7.3.210.0130.00716.56
7.3.200.0090.01019.39
7.3.190.0150.00316.58
7.3.180.0110.00616.56
7.3.170.0060.01316.54
7.3.160.0110.00516.39
7.3.120.0040.01414.82
7.2.330.0100.01016.85
7.2.320.0090.01516.53
7.2.310.0100.00916.60
7.2.300.0110.00716.59
7.2.290.0110.00716.58
7.2.00.0040.01119.58
7.1.100.0040.00717.83
7.1.70.0030.00717.33
7.1.60.0000.01216.95
7.1.50.0090.00316.64
7.1.00.0130.04722.40
7.0.200.0030.01316.61
7.0.140.0070.06722.04
7.0.100.0070.08020.02
7.0.90.0100.08319.97
7.0.80.0100.07320.01
7.0.70.0370.08319.97
7.0.60.0170.07719.97
7.0.50.0100.07720.34
7.0.40.0100.06020.08
7.0.30.0170.07020.13
7.0.20.0170.06020.10
7.0.10.0100.08319.91
7.0.00.0100.07720.09
5.6.280.0000.07721.08
5.6.250.0070.07720.50
5.6.240.0100.07720.68
5.6.230.0130.07720.66
5.6.220.0070.06720.58
5.6.210.0200.07320.65
5.6.200.0100.08021.15
5.6.190.0100.06321.04
5.6.180.0200.03320.95
5.6.170.0100.07721.01
5.6.160.0170.08020.99
5.6.150.0070.07321.04
5.6.140.0030.06021.11
5.6.130.0070.08321.01
5.6.120.0100.08321.03
5.6.110.0270.06021.17
5.6.100.0130.08721.02
5.6.90.0070.06321.01
5.6.80.0070.08320.56
5.6.70.0130.08020.34
5.6.60.0070.08020.34
5.6.50.0070.04320.45
5.6.40.0170.07720.36
5.6.30.0030.05020.41
5.6.20.0000.07020.43
5.6.10.0170.04320.31
5.6.00.0100.04020.39
5.5.380.0130.04320.41
5.5.370.0100.07320.52
5.5.360.0070.06320.51
5.5.350.0130.04320.47
5.5.340.0030.05720.90
5.5.330.0100.08020.80
5.5.320.0030.08720.80
5.5.310.0030.07720.93
5.5.300.0200.07020.92
5.5.290.0130.07720.89
5.5.280.0070.07320.94
5.5.270.0030.08720.82
5.5.260.0170.08020.88
5.5.250.0130.06320.61
5.5.240.0070.08320.11
5.5.230.0130.07720.27
5.5.220.0100.08020.27
5.5.210.0030.08320.27
5.5.200.0030.06020.12
5.5.190.0100.07020.18
5.5.180.0130.07720.18
5.5.160.0130.07320.17
5.5.150.0100.06320.28
5.5.140.0100.06320.19
5.5.130.0130.07020.24
5.5.120.0100.07320.27
5.5.110.0070.07719.99
5.5.100.0170.08020.16
5.5.90.0030.08020.18
5.5.80.0200.06720.05
5.5.70.0200.03320.16
5.5.60.0100.04720.12
5.5.50.0100.08320.04
5.5.40.0170.03320.16
5.5.30.0100.07720.05
5.5.20.0100.07320.05
5.5.10.0100.08020.14
5.5.00.0000.08019.95
5.4.450.0000.09019.18
5.4.440.0130.06719.36
5.4.430.0100.07319.24
5.4.420.0070.04019.55
5.4.410.0170.04019.32
5.4.400.0100.07018.95
5.4.390.0070.07718.94
5.4.380.0170.07319.13
5.4.370.0100.07019.16
5.4.360.0070.04019.18
5.4.350.0100.04019.13
5.4.340.0170.03319.23
5.4.320.0030.08018.85
5.4.310.0030.04019.23
5.4.300.0130.07319.05
5.4.290.0100.07318.90
5.4.280.0070.04019.14
5.4.270.0070.05719.16
5.4.260.0100.06019.13
5.4.250.0100.05319.20
5.4.240.0030.07318.91
5.4.230.0100.07319.22
5.4.220.0070.07318.84
5.4.210.0100.07319.17
5.4.200.0100.07719.15
5.4.190.0100.07019.17
5.4.180.0100.05319.03
5.4.170.0070.07319.15
5.4.160.0100.05718.86
5.4.150.0030.08319.10
5.4.140.0100.03016.46
5.4.130.0000.04716.54
5.4.120.0100.09316.54
5.4.110.0130.05316.50
5.4.100.0100.07016.34
5.4.90.0030.04316.48
5.4.80.0030.04016.39
5.4.70.0100.05316.55
5.4.60.0030.03716.42
5.4.50.0100.07016.33
5.4.40.0030.05016.56
5.4.30.0000.04016.44
5.4.20.0030.04316.47
5.4.10.0070.07716.46
5.4.00.0100.05315.89
5.3.290.0030.05314.76
5.3.280.0030.07714.61
5.3.270.0130.07014.61
5.3.260.0170.07314.68
5.3.250.0030.07014.71
5.3.240.0100.07314.74
5.3.230.0130.07014.64
5.3.220.0100.07314.59
5.3.210.0030.07014.72
5.3.200.0070.04714.48
5.3.190.0070.03714.55
5.3.180.0070.03314.62
5.3.170.0030.08014.52
5.3.160.0170.06014.55
5.3.150.0070.08014.55
5.3.140.0070.07314.66
5.3.130.0100.06314.68
5.3.120.0100.06314.45
5.3.110.0100.06314.61
5.3.100.0100.07314.17
5.3.90.0130.06714.12
5.3.80.0200.04714.15
5.3.70.0100.05014.14
5.3.60.0070.07714.10
5.3.50.0100.06713.95
5.3.40.0070.05713.95
5.3.30.0100.06013.84
5.3.20.0100.05313.80
5.3.10.0100.07313.66
5.3.00.0100.07013.79
5.2.170.0070.03011.57
5.2.160.0070.06011.57
5.2.150.0100.06011.57
5.2.140.0030.06011.57
5.2.130.0100.05311.57
5.2.120.0130.04711.57
5.2.110.0030.04011.57
5.2.100.0030.06011.57
5.2.90.0070.06311.57
5.2.80.0100.05311.57
5.2.70.0100.04011.57
5.2.60.0070.05011.57
5.2.50.0070.06011.57
5.2.40.0070.06011.57
5.2.30.0000.05711.57
5.2.20.0070.06011.57
5.2.10.0030.06311.57
5.2.00.0070.04311.57
5.1.60.0070.05011.57
5.1.50.0100.04711.57
5.1.40.0100.04711.57
5.1.30.0030.05311.57
5.1.20.0100.04711.57
5.1.10.0100.04311.57
5.1.00.0100.04711.57
5.0.50.0000.04711.57
5.0.40.0070.04011.57
5.0.30.0070.03311.59
5.0.20.0100.04011.62
5.0.10.0070.04311.62
5.0.00.0100.03711.62
4.4.90.0000.02711.57
4.4.80.0000.02011.57
4.4.70.0000.04011.57
4.4.60.0030.01711.57
4.4.50.0000.03711.57
4.4.40.0000.04311.57
4.4.30.0030.03711.57
4.4.20.0070.02011.57
4.4.10.0030.03711.57
4.4.00.0000.05711.57
4.3.110.0000.02011.58
4.3.100.0000.03711.61
4.3.90.0030.03011.62
4.3.80.0000.05311.62
4.3.70.0030.03711.62
4.3.60.0030.03311.62
4.3.50.0030.03311.62
4.3.40.0000.02311.62
4.3.30.0030.02011.62
4.3.20.0070.02711.62
4.3.10.0030.01711.62
4.3.00.0030.03311.62

preferences:
46.94 ms | 400 KiB | 5 Q