3v4l.org

run code in 300+ PHP versions simultaneously
<?php $list = json_decode(<<<'JSON' [ { "TagId": 2, "ParentTagId": null, "Name": "women" }, { "TagId": 5, "ParentTagId": 2, "Name": "bottom" }, { "TagId": 4, "ParentTagId": 2, "Name": "top" }, { "TagId": 7, "ParentTagId": 4, "Name": "shirt" }, { "TagId": 8, "ParentTagId": 4, "Name": "tshirt" }, { "TagId": 12, "ParentTagId": 7, "Name": "longsleeve" }, { "TagId": 16, "ParentTagId": null, "Name": "men" } ] JSON ); class Trie implements IteratorAggregate { protected $parent; protected $children = []; public function insert(Node $node) { $node->parent = $this; $this->children[] = $node; } public function findById($id) { foreach($this->children as $childNode) { if ($childNode->TagId === $id) { return $childNode; } } } public function hasChildren() { return (bool) count($this->children); } public function getIterator() { return $this->children; } } class Node extends Trie { public function __construct(stdClass $obj) { foreach($obj as $p => $v) { $this->$p = $v; } } } $trie = new Trie; /* Insert all of the parentless nodes */ foreach($list as $n => $obj) { if (!$obj->ParentTagId) { $trie->insert(new Node($obj)); unset($list[$n]); } } /* Insert all of the child nodes */ foreach($list as $n => $obj) { $p = $trie->findById($obj->ParentTagId); if ($p) { $p->insert(new Node($obj)); unset($list[$n]); } } foreach($trie as $node) var_dump(json_encode($node));

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.0150.00616.87
8.3.50.0110.00621.98
8.3.40.0120.00318.79
8.3.30.0180.00318.76
8.3.20.0080.00020.34
8.3.10.0030.00523.61
8.3.00.0050.00319.51
8.2.180.0140.00718.29
8.2.170.0070.00722.96
8.2.160.0110.00322.05
8.2.150.0050.00524.18
8.2.140.0000.00824.66
8.2.130.0030.00919.36
8.2.120.0080.00026.35
8.2.110.0030.00620.35
8.2.100.0040.00818.05
8.2.90.0050.00319.24
8.2.80.0040.00417.97
8.2.70.0030.00717.75
8.2.60.0000.01117.80
8.2.50.0060.00318.07
8.2.40.0030.00519.35
8.2.30.0040.00420.63
8.2.20.0060.00317.81
8.2.10.0040.00418.00
8.2.00.0040.00418.12
8.1.280.0090.00625.92
8.1.270.0090.00922.08
8.1.260.0070.00028.09
8.1.250.0040.00428.09
8.1.240.0000.01022.51
8.1.230.0040.01117.66
8.1.220.0000.00817.77
8.1.210.0080.00018.77
8.1.200.0030.00717.35
8.1.190.0000.00817.25
8.1.180.0070.00018.10
8.1.170.0000.00818.68
8.1.160.0080.00022.02
8.1.150.0000.00718.89
8.1.140.0040.00419.59
8.1.130.0040.00417.76
8.1.120.0000.00717.53
8.1.110.0040.00417.42
8.1.100.0080.00017.46
8.1.90.0000.00817.43
8.1.80.0000.00817.45
8.1.70.0000.00717.34
8.1.60.0040.00417.55
8.1.50.0040.00417.52
8.1.40.0000.00817.52
8.1.30.0060.00317.61
8.1.20.0040.00417.53
8.1.10.0050.00317.48
8.1.00.0000.00817.51
8.0.300.0050.00318.77
8.0.290.0000.00716.75
8.0.280.0040.00418.41
8.0.270.0060.00317.20
8.0.260.0030.00317.17
8.0.250.0070.00017.07
8.0.240.0050.00217.02
8.0.230.0030.00316.91
8.0.220.0030.00616.89
8.0.210.0000.00816.94
8.0.200.0000.00716.98
8.0.190.0030.00516.89
8.0.180.0060.00316.90
8.0.170.0090.00016.99
8.0.160.0040.00416.89
8.0.150.0030.00316.93
8.0.140.0000.00716.88
8.0.130.0030.00313.42
8.0.120.0000.00716.93
8.0.110.0050.00216.80
8.0.100.0030.00516.77
8.0.90.0080.00016.81
8.0.80.0080.00817.00
8.0.70.0000.00716.95
8.0.60.0040.00417.01
8.0.50.0000.00716.78
8.0.30.0150.00516.93
8.0.20.0130.00617.40
8.0.10.0030.00516.95
8.0.00.0040.01316.84
7.4.330.0030.00315.08
7.4.320.0030.00316.38
7.4.300.0060.00016.49
7.4.290.0030.00316.61
7.4.280.0040.00416.59
7.4.270.0040.00416.62
7.4.260.0000.00716.51
7.4.250.0040.00416.46
7.4.240.0020.00516.53
7.4.230.0030.00316.50
7.4.220.0070.01016.63
7.4.210.0060.01516.59
7.4.200.0030.00316.67
7.4.160.0120.00316.54
7.4.150.0070.01117.40
7.4.140.0100.01417.86
7.4.130.0100.01016.60
7.4.120.0100.00816.62
7.4.110.0120.00616.63
7.4.100.0060.01916.70
7.4.90.0080.01116.57
7.4.80.0180.00719.39
7.4.70.0100.01016.56
7.4.60.0060.00916.67
7.4.50.0000.00516.25
7.4.40.0060.01516.49
7.4.30.0120.00616.62
7.4.00.0090.00614.74
7.3.330.0050.00013.27
7.3.320.0060.00013.36
7.3.310.0030.00316.46
7.3.300.0000.00716.29
7.3.290.0050.01016.42
7.3.280.0100.00616.36
7.3.270.0110.00717.40
7.3.260.0070.01416.42
7.3.250.0060.01316.54
7.3.240.0060.01416.50
7.3.230.0140.00316.59
7.3.210.0030.01416.38
7.3.200.0090.00916.48
7.3.190.0090.00616.41
7.3.180.0070.01016.42
7.3.170.0160.00616.51
7.3.160.0100.00716.30
7.2.330.0130.01016.64
7.2.320.0130.01016.89
7.2.310.0040.01416.67
7.2.300.0070.01016.78
7.2.290.0090.00916.79
7.2.60.0070.00316.64
7.2.00.0090.00319.47
7.1.200.0160.00315.72
7.1.100.0000.01318.09
7.1.70.0040.00417.25
7.1.60.0110.01119.48
7.1.50.0100.01316.91
7.1.00.0070.07322.48
7.0.200.0000.00716.88
7.0.90.0100.05319.97
7.0.80.0030.04320.00
7.0.70.0230.07019.96
7.0.60.0170.06319.96
7.0.50.0130.08020.34
7.0.40.0370.06320.05
7.0.30.0030.05320.18
7.0.20.0100.06320.13
7.0.10.0030.07320.18
7.0.00.0100.07020.09
5.6.280.0030.07320.94
5.6.240.0100.07320.68
5.6.230.0070.05020.66
5.6.220.0030.05320.63
5.6.210.0070.08720.66
5.6.200.0100.06321.13
5.6.190.0130.03721.08
5.6.180.0130.07021.18
5.6.170.0130.07321.11
5.6.160.0100.08320.96
5.6.150.0030.04720.95
5.6.140.0030.08021.08
5.6.130.0100.06321.15
5.6.120.0100.08321.14
5.6.110.0100.08321.01
5.6.100.0030.06321.07
5.6.90.0070.06721.02
5.6.80.0100.04020.41
5.6.70.0000.05320.34
5.6.60.0070.05320.46
5.6.50.0070.04320.44
5.6.40.0030.04720.48
5.6.30.0070.07320.41
5.6.20.0100.07320.39
5.6.10.0000.06020.33
5.6.00.0030.08020.30
5.5.380.0030.06720.36
5.5.370.0130.07320.43
5.5.360.0070.08320.38
5.5.350.0170.08020.38
5.5.340.0030.08720.91
5.5.330.0100.04020.88
5.5.320.0100.05320.85
5.5.310.0100.07720.82
5.5.300.0070.08720.85
5.5.290.0170.07720.79
5.5.280.0000.08020.90
5.5.270.0130.07320.78
5.5.260.0070.08020.74
5.5.250.0170.06720.74
5.5.240.0100.06720.26
5.5.230.0070.07720.25
5.5.220.0130.06320.24
5.5.210.0000.09020.13
5.5.200.0070.08720.17
5.5.190.0100.04020.20
5.5.180.0070.08020.20
5.5.160.0030.08320.22
5.5.150.0100.08320.27
5.5.140.0130.07020.23
5.5.130.0130.07020.25
5.5.120.0000.06020.15
5.5.110.0100.05320.21
5.5.100.0030.07720.19
5.5.90.0170.06719.98
5.5.80.0100.07020.16
5.5.70.0170.06720.16
5.5.60.0070.05020.16
5.5.50.0100.07720.21
5.5.40.0170.06020.16
5.5.30.0170.07020.06
5.5.20.0070.08319.97
5.5.10.0130.06720.13
5.5.00.0170.06720.08

preferences:
76.09 ms | 401 KiB | 5 Q