3v4l.org

run code in 300+ PHP versions simultaneously
<?php // use ORDER BY belongs_to DESC, id ASC ... or usort() to prepare result set $resultset = [ ['id' => '6a', 'belongs_to' => '5a'], ['id' => '5a', 'belongs_to' => '3a'], ['id' => '8a', 'belongs_to' => '3a'], ['id' => '3a', 'belongs_to' => '1a'], ['id' => '1a', 'belongs_to' => null], ['id' => '2a', 'belongs_to' => null], ['id' => '4a', 'belongs_to' => null], ['id' => '7a', 'belongs_to' => null] ]; foreach ($resultset as $index1 => &$row1) { // make input array modifiable by reference (not working with a copy) if ($row1['belongs_to']) { // original belongs_to value is not null (not a top-level parent) foreach ($resultset as $index2 => $row2) { // search for targeted parent if ($row2['id'] == $row1['belongs_to']) { // parent found $resultset[$index2]['children'][] = [$row1['id'] => $row1['children'] ?? []]; // store original row as child unset($resultset[$index1]); // remove original row (no reason to iterate it again in outer loop) break; // halt inner loop (no reason to iterate further) } } } else { // original belongs_to value is null (top-level parent) $output[$row1['id']] = $row1['children'] ?? []; // store children to top } } var_export($output);

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.01418.06
8.3.50.0040.01116.16
8.3.40.0070.00718.47
8.3.30.0100.01018.59
8.3.20.0000.00719.19
8.3.10.0040.00421.70
8.3.00.0050.00320.57
8.2.180.0110.00718.05
8.2.170.0080.00822.96
8.2.160.0110.00422.14
8.2.150.0080.00024.18
8.2.140.0000.00824.66
8.2.130.0000.00819.95
8.2.120.0090.00026.35
8.2.110.0000.01120.35
8.2.100.0080.00417.78
8.2.90.0000.00817.63
8.2.80.0040.00417.97
8.2.70.0080.00018.03
8.2.60.0030.00618.16
8.2.50.0000.00818.10
8.2.40.0060.00319.48
8.2.30.0040.00419.37
8.2.20.0030.00518.01
8.2.10.0050.00318.01
8.2.00.0000.00817.77
8.1.280.0110.00625.92
8.1.270.0080.00022.11
8.1.260.0080.00026.35
8.1.250.0040.00428.09
8.1.240.0100.00023.86
8.1.230.0040.00823.98
8.1.220.0000.00817.78
8.1.210.0030.00618.77
8.1.200.0030.00617.35
8.1.190.0040.00417.38
8.1.180.0000.00818.10
8.1.170.0080.00018.46
8.1.160.0040.00418.83
8.1.150.0000.00818.89
8.1.140.0030.00322.17
8.1.130.0030.00320.28
8.1.120.0000.00717.46
8.1.110.0050.00217.44
8.1.100.0040.00417.43
8.1.90.0000.00717.43
8.1.80.0050.00317.44
8.1.70.0040.00417.42
8.1.60.0040.00417.52
8.1.50.0040.00417.42
8.1.40.0040.00417.54
8.1.30.0080.00317.55
8.1.20.0080.00017.55
8.1.10.0050.00317.52
8.1.00.0030.00617.34
8.0.300.0000.00818.77
8.0.290.0000.00816.63
8.0.280.0000.00818.45
8.0.270.0000.00717.14
8.0.260.0050.00318.46
8.0.250.0030.00316.96
8.0.240.0060.00016.88
8.0.230.0030.00316.98
8.0.220.0030.00616.89
8.0.210.0000.00716.91
8.0.200.0040.00416.95
8.0.190.0000.00716.85
8.0.180.0040.00416.93
8.0.170.0000.00816.84
8.0.160.0000.00716.79
8.0.150.0030.00716.81
8.0.140.0000.00716.77
8.0.130.0000.00513.34
8.0.120.0040.00416.81
8.0.110.0040.00416.89
8.0.100.0080.00016.75
8.0.90.0040.00416.96
8.0.80.0040.01216.87
8.0.70.0070.00016.79
8.0.60.0040.00416.87
8.0.50.0040.00416.75
8.0.30.0140.00317.05
8.0.20.0040.01517.40
8.0.10.0070.00017.04
8.0.00.0120.00616.76
7.4.330.0020.00215.55
7.4.320.0030.00316.45
7.4.300.0030.00316.58
7.4.290.0000.00716.52
7.4.280.0000.00816.60
7.4.270.0040.00416.61
7.4.260.0030.00316.52
7.4.250.0070.00016.48
7.4.240.0000.00816.63
7.4.230.0000.00816.43
7.4.220.0000.00816.50
7.4.210.0060.01016.57
7.4.200.0040.00416.36
7.4.160.0100.00716.54
7.4.140.0120.00617.86
7.4.130.0110.00616.63
7.4.120.0110.00916.58
7.4.110.0060.01216.55
7.4.100.0110.01216.45
7.4.90.0080.00916.78
7.4.80.0110.00819.39
7.4.70.0070.01016.47
7.4.60.0100.00616.54
7.4.50.0100.00716.52
7.4.40.0130.00916.55
7.4.00.0070.01114.74
7.3.330.0000.00513.12
7.3.320.0000.00513.26
7.3.310.0030.00316.39
7.3.300.0030.00316.25
7.3.290.0070.00016.34
7.3.280.0060.00916.34
7.3.260.0080.00916.40
7.3.240.0100.00916.56
7.3.230.0170.00016.69
7.3.210.0150.00716.40
7.3.200.0030.01316.41
7.3.190.0160.00716.34
7.3.180.0060.01616.40
7.3.170.0000.01616.58
7.3.160.0110.00616.73
7.2.330.0030.01316.79
7.2.320.0060.01416.58
7.2.310.0110.00716.53
7.2.300.0110.00416.59
7.2.290.0070.01416.63
7.2.110.1090.01014.80
7.2.100.1030.00314.73
7.2.90.0770.00714.70
7.2.80.0880.01014.63
7.2.70.0770.00714.89
7.2.60.0630.00614.48
7.2.50.0750.00915.14
7.2.40.0950.00614.79
7.2.30.0650.00914.85
7.2.20.0620.00915.06
7.2.10.0670.00315.05
7.2.00.0560.01314.98
7.1.230.1090.00614.09
7.1.220.0750.00713.92
7.1.210.1110.00314.05
7.1.200.0840.01013.65
7.1.190.0620.00313.92
7.1.180.0620.00713.76
7.1.170.0900.00713.95
7.1.160.0610.01014.04
7.1.150.0870.00713.88
7.1.140.0600.01313.86
7.1.130.0750.00514.05
7.1.120.0670.00313.96
7.1.110.0610.01013.90
7.1.100.0560.01113.68
7.1.90.0840.00713.76
7.1.80.0510.01313.57
7.1.70.0780.00013.90
7.1.60.0800.02231.86
7.1.50.1250.01031.88
7.1.40.1130.01331.88
7.1.30.1350.00431.70
7.1.20.1340.01031.74
7.1.10.0820.01413.77
7.1.00.0700.01013.59
5.6.380.0070.00713.84

preferences:
33.22 ms | 400 KiB | 5 Q