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; $out[$key]['children'] = array(); } else { //$parent = &find_by_key($sub['parent_id'], $out); //$parent['children'] = array(); //$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.0070.00718.55
8.3.50.0100.00621.91
8.3.40.0070.00718.91
8.3.30.0060.00918.75
8.3.20.0040.00418.91
8.3.10.0050.00321.14
8.3.00.0040.00718.83
8.2.180.0120.00316.88
8.2.170.0100.00722.96
8.2.160.0000.01420.39
8.2.150.0000.00824.18
8.2.140.0040.00424.66
8.2.130.0000.00826.16
8.2.120.0040.00420.93
8.2.110.0110.00720.48
8.2.100.0130.00017.84
8.2.90.0030.00519.20
8.2.80.0040.00417.97
8.2.70.0070.00417.38
8.2.60.0030.00517.93
8.2.50.0030.00618.07
8.2.40.0080.00018.34
8.2.30.0000.00821.23
8.2.20.0000.00817.65
8.2.10.0040.00417.74
8.2.00.0050.00317.79
8.1.280.0100.00625.92
8.1.270.0030.00623.99
8.1.260.0060.00326.35
8.1.250.0000.00828.09
8.1.240.0070.01022.46
8.1.230.0080.00320.88
8.1.220.0040.00418.77
8.1.210.0080.00018.95
8.1.200.0030.00717.35
8.1.190.0080.00017.54
8.1.180.0000.00818.10
8.1.170.0050.00318.64
8.1.160.0080.00018.84
8.1.150.0000.00718.72
8.1.140.0000.00817.48
8.1.130.0040.00417.74
8.1.120.0000.00717.50
8.1.110.0000.00817.54
8.1.100.0050.00317.54
8.1.90.0040.00417.51
8.1.80.0080.00017.49
8.1.70.0070.00017.51
8.1.60.0040.00417.58
8.1.50.0040.00417.43
8.1.40.0000.00717.52
8.1.30.0040.00417.56
8.1.20.0040.00417.58
8.1.10.0040.00717.50
8.1.00.0000.00817.50
8.0.300.0080.00019.96
8.0.290.0000.00717.04
8.0.280.0030.00318.41
8.0.270.0070.00017.36
8.0.260.0030.00316.76
8.0.250.0040.00417.00
8.0.240.0040.00416.82
8.0.230.0050.00216.97
8.0.220.0030.00316.91
8.0.210.0000.00716.96
8.0.200.0000.00616.89
8.0.190.0030.00616.99
8.0.180.0070.00016.91
8.0.170.0000.00817.02
8.0.160.0000.00716.84
8.0.150.0070.00016.92
8.0.140.0000.00716.85
8.0.130.0040.00413.38
8.0.120.0040.00416.88
8.0.110.0080.00016.95
8.0.100.0070.00017.00
8.0.90.0040.00416.81
8.0.80.0040.01116.94
8.0.70.0040.00416.73
8.0.60.0020.00516.98
8.0.50.0030.00516.90
8.0.30.0150.00617.29
8.0.20.0180.00917.40
8.0.10.0040.00417.09
8.0.00.0110.00916.82
7.4.330.0000.00516.84
7.4.320.0030.00316.60
7.4.300.0030.00316.64
7.4.290.0050.00216.55
7.4.280.0000.00816.52
7.4.270.0000.00716.66
7.4.260.0000.00613.21
7.4.250.0080.00016.65
7.4.240.0030.00316.49
7.4.230.0000.00716.74
7.4.220.0120.00616.49
7.4.210.0070.00816.55
7.4.200.0070.00016.74
7.4.190.0000.00816.62
7.4.160.0160.00316.55
7.4.150.0110.00717.40
7.4.140.0040.01417.86
7.4.130.0110.00916.53
7.4.120.0090.00816.46
7.4.110.0100.00716.84
7.4.100.0070.01316.56
7.4.90.0160.00316.41
7.4.80.0070.01019.39
7.4.70.0090.00916.69
7.4.60.0070.01716.52
7.4.50.0030.00616.19
7.4.40.0040.01522.77
7.4.30.0160.00316.69
7.4.00.0090.00714.95
7.3.330.0070.00013.34
7.3.320.0060.00013.38
7.3.310.0030.00316.43
7.3.300.0000.00716.39
7.3.290.0130.00016.37
7.3.280.0070.01116.35
7.3.270.0100.01017.40
7.3.260.0100.01018.24
7.3.250.0090.00916.53
7.3.240.0090.00616.60
7.3.230.0070.01016.41
7.3.210.0090.00916.46
7.3.200.0060.01319.39
7.3.190.0120.01216.35
7.3.180.0070.01016.36
7.3.170.0130.00916.37
7.3.160.0130.00416.47
7.3.120.0110.00514.92
7.3.110.0070.01214.95
7.3.100.0080.00814.80
7.3.90.0080.00614.88
7.3.80.0040.00915.03
7.3.70.0080.00214.74
7.3.60.0050.01014.95
7.3.50.0060.00614.80
7.3.40.0050.01014.69
7.3.30.0040.00914.89
7.3.20.0090.00616.71
7.3.10.0050.00616.56
7.3.00.0050.01116.60
7.2.330.0130.00616.78
7.2.320.0140.00316.83
7.2.310.0200.00316.50
7.2.300.0090.00916.59
7.2.290.0070.01016.58
7.2.250.0090.00915.23
7.2.240.0110.00715.11
7.2.230.0050.00815.09
7.2.220.0070.00715.11
7.2.210.0020.01115.17
7.2.200.0030.00815.22
7.2.190.0010.01215.14
7.2.180.0040.00715.08
7.2.170.0040.01014.98
7.2.160.0040.00715.04
7.2.150.0080.00816.86
7.2.140.0120.00316.71
7.2.130.0150.00916.83
7.2.120.0050.01216.57
7.2.110.0080.00916.75
7.2.100.0100.00616.83
7.2.90.0060.00716.92
7.2.80.0030.01317.01
7.2.70.0050.00816.99
7.2.60.0040.01016.64
7.2.50.0070.00616.79
7.2.40.0060.00816.95
7.2.30.0060.00916.81
7.2.20.0080.00416.92
7.2.10.0130.00616.81
7.2.00.0070.00916.99
7.1.330.0080.00715.92
7.1.320.0100.00215.85
7.1.310.0050.01115.69
7.1.300.0050.00715.64
7.1.290.0050.00715.73
7.1.280.0070.00515.61
7.1.270.0060.00815.82
7.1.260.0050.00915.89
7.1.250.0080.01015.44
7.1.240.0110.00415.94
7.1.230.0090.00315.90
7.1.220.0040.01215.61
7.1.210.0080.00015.54
7.1.200.0080.00415.56
7.1.190.0000.01415.88
7.1.180.0060.00615.70
7.1.170.0070.00415.89
7.1.160.0030.00715.92
7.1.150.0060.00915.88
7.1.140.0030.01015.65
7.1.130.0090.00315.82
7.1.120.0060.00615.86
7.1.110.0110.00415.68
7.1.100.0000.01015.99
7.1.90.0110.00415.57
7.1.80.0040.01115.88
7.1.70.0040.00516.44
7.1.60.0040.00916.51
7.1.50.0080.00716.24
7.1.40.0070.01016.00
7.1.30.0000.01215.89
7.1.20.0040.00715.91
7.1.10.0040.00715.61
7.1.00.0020.03619.18
7.0.330.0150.00015.11
7.0.320.0030.00915.45
7.0.310.0030.01015.52
7.0.300.0060.00315.43
7.0.290.0110.00415.54
7.0.280.0030.01615.35
7.0.270.0040.00915.42
7.0.260.0030.01415.40
7.0.250.0050.00815.58
7.0.240.0100.00315.31
7.0.230.0080.00415.52
7.0.220.0030.00515.58
7.0.210.0030.00715.46
7.0.200.0000.01116.28
7.0.190.0060.01015.43
7.0.180.0070.00715.34
7.0.170.0040.00815.54
7.0.160.0100.00715.43
7.0.150.0040.00815.59
7.0.140.0070.03718.76
7.0.130.0000.01315.61
7.0.120.0090.00315.36
7.0.110.0100.00015.41
7.0.100.0050.04117.69
7.0.90.0320.04217.62
7.0.80.0070.04417.62
7.0.70.0210.04317.64
7.0.60.0280.04417.69
7.0.50.0220.04217.83
7.0.40.0050.04516.74
7.0.30.0020.04716.73
7.0.20.0020.04516.75
7.0.10.0100.04316.77
7.0.00.0060.02716.80
5.6.400.0060.00614.48
5.6.390.0000.00914.68
5.6.380.0070.00714.79
5.6.370.0060.00614.47
5.6.360.0030.01414.38
5.6.350.0110.00714.18
5.6.340.0060.01314.44
5.6.330.0040.01114.36
5.6.320.0040.00814.17
5.6.310.0090.00614.77
5.6.300.0060.00614.05
5.6.290.0040.01114.75
5.6.280.0080.03817.61
5.6.270.0090.00614.43
5.6.260.0000.01514.74
5.6.250.0120.04517.53
5.6.240.0110.04017.59
5.6.230.0100.02317.55
5.6.220.0070.04017.47
5.6.210.0020.04717.66
5.6.200.0070.04117.63
5.6.190.0040.03217.71
5.6.180.0130.03817.71
5.6.170.0080.04517.74
5.6.160.0050.03217.67
5.6.150.0110.03517.82
5.6.140.0070.04617.88
5.6.130.0140.03817.73
5.6.120.0090.04217.85
5.6.110.0080.04517.85
5.6.100.0020.03117.69
5.6.90.0020.03217.67
5.6.80.0050.02617.42
5.6.70.0040.02517.30
5.6.60.0030.02517.34
5.6.50.0100.02317.42
5.6.40.0030.03817.31
5.6.30.0080.02317.37
5.6.20.0060.02017.32
5.6.10.0030.03017.45
5.6.00.0100.02717.43
5.5.380.0150.02817.38
5.5.370.0110.03617.48
5.5.360.0070.04417.54
5.5.350.0090.04217.53
5.5.340.0050.04217.82
5.5.330.0100.02517.53
5.5.320.0080.02817.66
5.5.310.0100.04017.58
5.5.300.0100.04017.62
5.5.290.0090.04217.57
5.5.280.0050.04717.63
5.5.270.0080.04817.70
5.5.260.0080.04117.57
5.5.250.0020.02817.39
5.5.240.0030.02517.25
5.5.230.0050.02317.28
5.5.220.0090.01717.28
5.5.210.0040.02817.17
5.5.200.0000.03117.30
5.5.190.0050.02317.24
5.5.180.0050.02317.31
5.5.170.0100.00314.45
5.5.160.0030.03117.23
5.5.150.0090.04117.21
5.5.140.0030.03917.10
5.5.130.0080.02017.33
5.5.120.0010.02817.37
5.5.110.0030.03017.18
5.5.100.0080.02517.31
5.5.90.0020.02717.15
5.5.80.0020.02517.18
5.5.70.0050.02217.26
5.5.60.0030.02717.18
5.5.50.0080.02217.24
5.5.40.0050.02217.12
5.5.30.0050.02317.25
5.5.20.0070.02217.18
5.5.10.0060.02017.11
5.5.00.0030.02317.22
5.4.450.0080.03015.30
5.4.440.0030.04515.35
5.4.430.0070.03815.09
5.4.420.0080.02215.22
5.4.410.0050.02515.23
5.4.400.0030.02214.97
5.4.390.0030.02115.28
5.4.380.0070.02015.10
5.4.370.0020.02315.05
5.4.360.0050.02115.21
5.4.350.0050.02315.04
5.4.340.0030.02315.14
5.4.330.0000.00811.39
5.4.320.0070.02315.07
5.4.310.0100.01215.00
5.4.300.0050.02715.05
5.4.290.0020.02415.09
5.4.280.0040.02015.01
5.4.270.0060.01815.15
5.4.260.0020.02315.25
5.4.250.0070.01814.96
5.4.240.0030.02214.97
5.4.230.0070.01915.16
5.4.220.0080.02014.88
5.4.210.0060.02015.15
5.4.200.0060.02015.18
5.4.190.0030.02314.99
5.4.180.0050.02115.24
5.4.170.0070.01915.29
5.4.160.0020.02215.09
5.4.150.0090.01515.17
5.4.140.0060.02013.74
5.4.130.0070.01713.96
5.4.120.0070.01713.63
5.4.110.0000.02213.73
5.4.100.0020.01913.77
5.4.90.0020.03913.80
5.4.80.0050.03013.98
5.4.70.0020.03313.75
5.4.60.0080.03113.86
5.4.50.0040.03713.70
5.4.40.0040.02613.81
5.4.30.0080.03313.70
5.4.20.0020.04013.73
5.4.10.0080.02313.83
5.4.00.0030.04113.47
5.3.290.0030.02312.70
5.3.280.0050.02112.54
5.3.270.0070.01812.56
5.3.260.0040.02012.62
5.3.250.0070.01812.74
5.3.240.0050.02012.65
5.3.230.0050.02012.64
5.3.220.0050.01812.61
5.3.210.0080.03112.63
5.3.200.0050.03212.62
5.3.190.0050.03812.52
5.3.180.0050.03612.66
5.3.170.0060.02812.65
5.3.160.0070.04012.62
5.3.150.0030.02612.51
5.3.140.0030.02812.72
5.3.130.0080.03812.62
5.3.120.0060.03812.60
5.3.110.0030.04012.52
5.3.100.0040.04312.42
5.3.90.0050.04212.52
5.3.80.0050.04212.50
5.3.70.0030.02512.44
5.3.60.0050.03512.38
5.3.50.0070.03612.32
5.3.40.0050.04412.48
5.3.30.0050.03012.46
5.3.20.0060.03512.37
5.3.10.0050.03912.36
5.3.00.0040.03212.36
5.2.170.0030.05314.32
5.2.160.0100.05314.32
5.2.150.0100.03314.32
5.2.140.0070.05714.32
5.2.130.0100.06014.32
5.2.120.0030.04314.32
5.2.110.0070.06014.32
5.2.100.0030.03314.32
5.2.90.0030.05714.32
5.2.80.0100.03014.32
5.2.70.0000.06714.32
5.2.60.0030.04314.32
5.2.50.0030.06314.32
5.2.40.0070.05714.32
5.2.30.0030.03714.32
5.2.20.0070.05314.32
5.2.10.0100.04714.32
5.2.00.0030.05014.32
5.1.60.0030.05014.32
5.1.50.0070.04014.32
5.1.40.0070.05014.32
5.1.30.0070.02714.32
5.1.20.0000.04714.32
5.1.10.0130.04714.32
5.1.00.0030.04314.32
5.0.50.0070.04014.32
5.0.40.0030.03314.32
5.0.30.0000.07014.32
5.0.20.0000.04714.32
5.0.10.0030.03314.32
5.0.00.0000.04314.32
4.4.90.0100.02714.32
4.4.80.0030.03314.32
4.4.70.0000.03714.32
4.4.60.0000.03714.32
4.4.50.0000.03714.32
4.4.40.0000.03014.32
4.4.30.0000.03714.32
4.4.20.0100.03314.32
4.4.10.0130.02014.32
4.4.00.0000.05314.32
4.3.110.0000.03714.32
4.3.100.0000.03314.32
4.3.90.0000.03714.32
4.3.80.0000.05014.32
4.3.70.0000.03714.32
4.3.60.0030.03014.32
4.3.50.0000.02014.32
4.3.40.0000.05014.32
4.3.30.0000.03314.32
4.3.20.0030.03314.32
4.3.10.0070.03014.32
4.3.00.0030.02714.32

preferences:
51.55 ms | 401 KiB | 5 Q