3v4l.org

run code in 300+ PHP versions simultaneously
<?php $resultset = [ ['id' => '1a', 'parent' => null], ['id' => '2a', 'parent' => null], ['id' => '3a', 'parent' => '1a'], ['id' => '4a', 'parent' => null], ['id' => '5a', 'parent' => '3a'], ['id' => '6a', 'parent' => '5a'], ['id' => '7a', 'parent' => null], ['id' => '8a', 'parent' => '3a'] ]; function findParent(&$array, $parent = 0, $children = []){ // make $array modifiable foreach ($array as $i => &$row) { // make $row modifiable if ($parent) { // if not zero if ($i == $parent) { // found parent unset($array[$i]['parent']); $row = $children; // append child to parent's children subarray } elseif (!empty($row)) { // go down rabbit hole looking for parent findParent($row, $parent, $children); // look deeper for parent while preserving the initial parent_id and row // $row = $row['children']; } // else continue; } elseif ($row['parent']) { // child requires adoption unset($array[$i]); // remove child from level because it will be store elsewhere and won't be its own parent (reduce iterations in next loop & avoid infinite recursion) findParent($array, $row['parent'], $row); // look for parent using parent_id while carrying the entire row as the childarray } // else continue; } return $array; // return the modified array } var_export(findParent($resultset));

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.0160.00618.43
8.3.50.0100.01018.29
8.3.40.0070.00718.79
8.3.30.0070.00718.62
8.3.20.0000.00819.33
8.3.10.0080.00023.67
8.3.00.0030.00520.64
8.2.180.0120.00616.50
8.2.170.0070.01422.96
8.2.160.0080.00622.31
8.2.150.0050.00324.18
8.2.140.0000.00824.66
8.2.130.0000.00818.21
8.2.120.0040.00426.35
8.2.110.0090.00019.38
8.2.100.0080.00417.84
8.2.90.0060.00317.88
8.2.80.0040.00417.97
8.2.70.0000.00818.16
8.2.60.0020.00518.22
8.2.50.0040.00418.10
8.2.40.0070.00019.46
8.2.30.0030.00519.20
8.2.20.0040.00418.11
8.2.10.0000.00718.00
8.2.00.0040.00417.64
8.1.280.0090.00625.92
8.1.270.0080.00018.88
8.1.260.0050.00326.35
8.1.250.0000.00828.09
8.1.240.0100.00022.66
8.1.230.0040.00817.42
8.1.220.0060.00317.74
8.1.210.0000.00818.77
8.1.200.0030.00917.47
8.1.190.0040.00417.23
8.1.180.0000.00818.10
8.1.170.0040.00418.46
8.1.160.0000.00718.80
8.1.150.0000.00718.95
8.1.140.0050.00322.05
8.1.130.0000.00720.14
8.1.120.0000.00717.43
8.1.110.0040.00417.50
8.1.100.0050.00317.49
8.1.90.0020.00517.50
8.1.80.0040.00417.45
8.1.70.0000.00717.53
8.1.60.0000.00817.60
8.1.50.0000.00817.51
8.1.40.0030.00617.54
8.1.30.0050.00517.72
8.1.20.0020.00517.64
8.1.10.0040.00417.61
8.1.00.0040.00417.53
8.0.300.0000.00718.77
8.0.290.0000.00816.58
8.0.280.0000.00718.46
8.0.270.0000.00817.26
8.0.260.0000.00618.33
8.0.250.0000.00717.04
8.0.240.0070.00017.00
8.0.230.0040.00416.91
8.0.220.0050.00316.95
8.0.210.0000.00816.95
8.0.200.0070.00016.99
8.0.190.0070.00016.92
8.0.180.0000.00817.04
8.0.170.0030.00716.92
8.0.160.0040.00416.98
8.0.150.0040.00416.79
8.0.140.0080.00016.90
8.0.130.0060.00013.43
8.0.120.0000.00816.98
8.0.110.0060.00316.96
8.0.100.0070.00016.84
8.0.90.0000.00816.86
8.0.80.0020.01416.92
8.0.70.0030.00517.07
8.0.60.0030.00516.91
8.0.50.0000.00716.85
8.0.30.0140.00617.20
8.0.20.0080.01217.40
8.0.10.0000.00717.07
8.0.00.0090.01016.82
7.4.330.0000.00615.55
7.4.320.0000.00616.61
7.4.300.0070.00016.62
7.4.290.0000.00816.61
7.4.280.0080.00016.52
7.4.270.0030.00316.44
7.4.260.0000.00816.52
7.4.250.0030.00516.49
7.4.240.0050.00316.53
7.4.230.0030.00316.71
7.4.220.0040.00416.44
7.4.210.0050.01216.46
7.4.200.0030.00316.47
7.4.160.0100.00616.57
7.4.140.0110.00917.86
7.4.130.0040.01316.63
7.4.120.0130.00816.56
7.4.110.0150.00616.56
7.4.100.0040.01216.52
7.4.90.0090.00916.50
7.4.80.0180.00719.39
7.4.70.0120.00616.59
7.4.60.0060.00916.38
7.4.50.0030.00816.68
7.4.40.0110.00616.32
7.4.00.0070.00714.94
7.3.330.0050.00013.27
7.3.320.0000.00513.42
7.3.310.0040.00416.50
7.3.300.0000.00716.37
7.3.290.0040.00416.34
7.3.280.0060.01116.44
7.3.260.0120.00716.50
7.3.240.0120.00716.44
7.3.230.0170.00716.36
7.3.210.0070.01016.56
7.3.200.0080.00816.46
7.3.190.0120.01016.63
7.3.180.0060.01116.41
7.3.170.0070.01616.63
7.3.160.0130.00316.41
7.2.330.0180.01216.79
7.2.320.0050.01516.73
7.2.310.0100.00716.41
7.2.300.0140.00316.59
7.2.290.0060.01316.52
7.2.110.0070.00716.48
7.2.100.0060.00616.55
7.2.90.0110.00316.50
7.2.80.0030.01316.73
7.2.70.0090.00316.77
7.2.60.0030.01016.71
7.2.50.0070.00716.74
7.2.40.0060.00616.98
7.2.30.0080.00816.95
7.2.20.0120.00316.66
7.2.10.0100.00716.79
7.2.00.0080.00816.61
7.1.230.0040.00715.76
7.1.220.0050.00815.82
7.1.210.0070.00715.48
7.1.200.0090.00615.94
7.1.190.0060.00615.69
7.1.180.0000.01215.52
7.1.170.0000.01415.81
7.1.160.0050.00715.49
7.1.150.0040.00715.64
7.1.140.0030.01015.76
7.1.130.0110.00315.59
7.1.120.0030.01015.89
7.1.110.0100.00315.41
7.1.100.0090.00615.66
7.1.90.0040.01215.66
7.1.80.0090.00315.78
7.1.70.0070.00615.80
7.1.60.0200.01033.43
7.1.50.0300.00933.59
7.1.40.0310.00633.71
7.1.30.0200.02033.42
7.1.20.0230.01633.66
7.1.10.0040.01115.61
7.1.00.0080.00715.52
5.6.380.0090.00614.44

preferences:
74.48 ms | 400 KiB | 5 Q