3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array = [ "# Grandparent #1", "# Grandparent #2", "## Parent #2-1", "### Child #2-1-1", "## Parent #2-2", "### Child #2-2-1", "### Child #2-2-2", "## Parent #2-3", "## Parent #2-4", "# Grandparent #3", "## Parent #3-1", ]; function recurse(array $lines, int $level = 1): array { $tree = []; $directDescendants = []; foreach($lines as $line) { [$hashes, $name] = explode(' ', $line, 2); $depth = strlen($hashes); if ($depth === $level) { if ($directDescendants) { $parent['children'] = recurse($directDescendants, $level + 1); } $directDescendants = []; // reset collection of direct descendants unset($parent); // destroy reference $parent = ['name' => $name]; // name the member $tree[] = &$parent; // push the reference variable into the tree } elseif ($depth > $level) { $directDescendants[] = $line; // collect only direct descendants of current level parent member } } if ($directDescendants) { $parent['children'] = recurse($directDescendants, $level + 1); } return $tree; } var_export(recurse($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.5.10.0110.00616.58
8.5.00.0110.01020.07
8.4.150.0030.00014.05
8.4.140.0160.00717.58
8.4.130.0110.00919.36
8.4.120.0120.00820.40
8.4.110.0120.00822.39
8.4.100.0100.00717.53
8.4.90.0130.00720.40
8.4.80.0040.00618.95
8.4.70.0110.01020.71
8.4.60.0130.00620.63
8.4.50.0070.01317.76
8.4.40.0130.00717.71
8.4.30.0180.00420.43
8.4.20.0100.01024.09
8.4.10.0060.00319.23
8.3.280.0150.00618.45
8.3.270.0150.00516.62
8.3.260.0120.00716.48
8.3.250.0050.00319.09
8.3.240.0140.00516.79
8.3.230.0090.00916.46
8.3.220.0120.00719.10
8.3.210.0110.00916.60
8.3.200.0060.00316.77
8.3.190.0090.00816.49
8.3.180.0090.00716.85
8.3.170.0060.01216.54
8.3.160.0110.00718.29
8.3.150.0130.00617.12
8.3.140.0060.00316.57
8.3.130.0150.00016.33
8.3.120.0000.00919.03
8.3.110.0060.00320.94
8.3.100.0070.01024.06
8.3.90.0080.00026.77
8.3.80.0000.01016.75
8.3.70.0000.01616.58
8.3.60.0210.00016.72
8.3.50.0090.01023.59
8.3.40.0030.01421.80
8.3.30.0120.00318.85
8.3.20.0080.00024.18
8.3.10.0040.00424.66
8.3.00.0080.00026.16
8.2.290.0090.00920.54
8.2.280.0130.00720.17
8.2.270.0110.00716.84
8.2.260.0060.00318.98
8.2.250.0040.00416.51
8.2.240.0120.00617.07
8.2.230.0050.00522.58
8.2.220.0060.00337.54
8.2.210.0090.00926.77
8.2.200.0040.00716.35
8.2.190.0090.01216.75
8.2.180.0120.00625.92
8.2.170.0120.00618.93
8.2.160.0030.01022.96
8.2.150.0000.00825.66
8.2.140.0000.00824.66
8.2.130.0000.00826.16
8.2.120.0040.00419.80
8.2.110.0100.00020.93
8.2.100.0000.01117.84
8.2.90.0000.00917.88
8.2.80.0060.00317.97
8.2.70.0040.00818.04
8.2.60.0040.00417.91
8.2.50.0080.00018.10
8.2.40.0040.00419.84
8.2.30.0040.00418.12
8.2.20.0000.00718.14
8.2.10.0040.00419.32
8.2.00.0020.00519.25
8.1.330.0100.00921.90
8.1.320.0080.01118.47
8.1.310.0040.00416.77
8.1.300.0000.00918.05
8.1.290.0080.00018.88
8.1.280.0040.01125.92
8.1.270.0000.00822.14
8.1.260.0000.00826.35
8.1.250.0000.00828.09
8.1.240.0100.00317.46
8.1.230.0030.00920.91
8.1.220.0040.00417.74
8.1.210.0030.00518.77
8.1.200.0070.00317.35
8.1.190.0050.00317.23
8.1.180.0040.00418.10
8.1.170.0060.00319.01
8.1.160.0060.00320.58
8.1.150.0070.00018.82
8.1.140.0000.00718.80
8.1.130.0000.00720.07
8.1.120.0070.00017.51
8.1.110.0000.00817.46
8.1.100.0050.00317.48
8.1.90.0000.00717.45
8.1.80.0050.00517.42
8.1.70.0000.00717.35
8.1.60.0080.00017.59
8.1.50.0060.00317.42
8.1.40.0040.00417.50
8.1.30.0040.00417.53
8.1.20.0090.00017.69
8.1.10.0060.00317.55
8.1.00.0040.00417.48
8.0.300.0040.00419.60
8.0.290.0040.00416.63
8.0.280.0040.00418.38
8.0.270.0040.00418.00
8.0.260.0030.00318.46
8.0.250.0030.00316.86
8.0.240.0070.00016.92
8.0.230.0030.00316.95
8.0.220.0070.00016.90
8.0.210.0000.00716.79
8.0.200.0070.00016.89
8.0.190.0070.00016.98
8.0.180.0040.00416.82
8.0.170.0000.00816.81
8.0.160.0000.00816.92
8.0.150.0040.00416.88
8.0.140.0000.00716.85
8.0.130.0000.00513.38
8.0.120.0000.00816.73
8.0.110.0000.00816.86
8.0.100.0040.00416.96
8.0.90.0030.00516.78
8.0.80.0070.00716.85
8.0.70.0060.00316.80
8.0.60.0040.00416.91
8.0.50.0000.00716.97
8.0.30.0040.01317.13
8.0.20.0130.00817.09
8.0.10.0080.00016.89
8.0.00.0130.00716.82
7.4.330.0050.00015.55
7.4.320.0030.00316.50
7.4.300.0070.00016.49
7.4.290.0040.00416.61
7.4.280.0000.00716.60
7.4.270.0050.00216.42
7.4.260.0000.00513.29
7.4.250.0000.00716.54
7.4.240.0000.00816.62
7.4.230.0040.00416.53
7.4.220.0000.00716.57
7.4.210.0080.00816.57
7.4.200.0040.00416.59
7.4.130.0080.01116.55
7.4.120.0030.01416.51
7.4.110.0070.01016.66
7.4.100.0080.01216.23
7.4.90.0130.00316.34
7.4.80.0170.00016.45
7.4.70.0030.01416.49
7.4.60.0100.01016.44
7.4.50.0100.01016.36
7.4.40.0090.01516.55
7.4.30.0060.01816.50
7.4.20.0040.01416.03
7.4.10.0110.00716.53
7.4.00.0130.00916.43
7.3.330.0000.00716.40
7.3.320.0030.00313.27
7.3.310.0050.00216.36
7.3.300.0000.00616.25
7.3.290.0070.00916.39
7.3.280.0040.01116.34
7.3.260.0030.01516.58
7.3.230.0260.02616.59
7.3.220.0220.00016.12
7.3.210.0100.01016.54
7.3.200.0100.00616.32
7.3.190.0110.00716.30
7.3.180.0160.00616.35
7.3.170.0120.00616.22
7.3.160.0130.00616.18
7.3.150.0140.00516.39
7.3.140.0030.01516.20
7.3.130.0130.00616.12
7.3.120.0090.01216.41
7.3.110.0120.00916.34
7.3.100.0110.00816.24
7.3.90.0150.00416.20
7.3.80.0070.01616.22
7.3.70.0210.00316.51
7.3.60.0090.00916.32
7.3.50.0100.00716.42
7.3.40.0000.02316.50
7.3.30.0100.00716.13
7.3.20.0110.00716.29
7.3.10.0110.00716.05
7.3.00.0060.01516.36
7.2.330.0070.01016.67
7.2.320.0090.00916.55
7.2.310.0120.00616.53
7.2.300.0180.00316.71
7.2.290.0130.00916.52
7.2.280.0070.01316.41
7.2.270.0060.01316.75
7.2.260.0130.01016.53
7.2.250.0060.01416.65
7.2.240.0100.00716.75
7.2.230.0110.00816.52
7.2.220.0110.01116.52
7.2.210.0030.02016.49
7.2.200.0090.00916.45
7.2.190.0040.01516.36
7.2.180.0120.01216.42
7.2.170.0110.00816.64
7.2.160.0130.00616.68
7.2.150.0090.00916.63
7.2.140.0120.00916.34
7.2.130.0120.01216.48
7.2.120.0150.00716.60
7.2.110.0070.01116.52
7.2.100.0080.01216.33
7.2.90.0270.00616.55
7.2.80.0040.01416.65
7.2.70.0090.00916.36
7.2.60.0060.01216.48
7.2.50.0110.00816.43
7.2.40.0060.01216.64
7.2.30.0090.01216.53
7.2.20.0100.01016.57
7.2.10.0040.01416.39
7.2.00.0040.01416.55

preferences:
142.5 ms | 403 KiB | 5 Q