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 { 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; } } } } 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]); } } var_dump($trie);

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.0110.00716.50
8.3.50.0110.00921.99
8.3.40.0160.00018.69
8.3.30.0110.00418.82
8.3.20.0050.00320.21
8.3.10.0080.00023.50
8.3.00.0060.00319.50
8.2.180.0180.00418.26
8.2.170.0070.00722.96
8.2.160.0140.00022.14
8.2.150.0040.00424.18
8.2.140.0000.00724.66
8.2.130.0080.00417.63
8.2.120.0060.00326.35
8.2.110.0040.00420.54
8.2.100.0040.00817.93
8.2.90.0030.00519.34
8.2.80.0000.00817.97
8.2.70.0080.00017.63
8.2.60.0030.00518.05
8.2.50.0000.00818.07
8.2.40.0050.00319.29
8.2.30.0040.00420.65
8.2.20.0060.00317.89
8.2.10.0000.00718.18
8.2.00.0040.00418.02
8.1.280.0070.00725.92
8.1.270.0030.00522.14
8.1.260.0040.00428.09
8.1.250.0030.00528.09
8.1.240.0000.00922.59
8.1.230.0120.00017.79
8.1.220.0050.00317.74
8.1.210.0060.00618.77
8.1.200.0030.00617.35
8.1.190.0050.00317.12
8.1.180.0000.00818.10
8.1.170.0050.00518.50
8.1.160.0070.00021.98
8.1.150.0040.00418.91
8.1.140.0030.00519.59
8.1.130.0040.00417.76
8.1.120.0070.00017.45
8.1.110.0050.00517.51
8.1.100.0050.00317.34
8.1.90.0050.00317.52
8.1.80.0000.00817.49
8.1.70.0040.00417.38
8.1.60.0040.00417.64
8.1.50.0000.00817.43
8.1.40.0000.00917.50
8.1.30.0080.00017.64
8.1.20.0040.00417.57
8.1.10.0050.00317.59
8.1.00.0000.00817.39
8.0.300.0000.00818.77
8.0.290.0000.00716.75
8.0.280.0030.00318.52
8.0.270.0000.00817.22
8.0.260.0000.00617.39
8.0.250.0000.00717.03
8.0.240.0030.00316.93
8.0.230.0040.00317.05
8.0.220.0080.00017.00
8.0.210.0030.00516.98
8.0.200.0030.00316.99
8.0.190.0040.00416.91
8.0.180.0040.00417.05
8.0.170.0050.00316.88
8.0.160.0030.00316.94
8.0.150.0000.00916.97
8.0.140.0040.00416.91
8.0.130.0030.00313.38
8.0.120.0040.00416.95
8.0.110.0030.00617.02
8.0.100.0040.00416.76
8.0.90.0000.00717.04
8.0.80.0090.00917.00
8.0.70.0040.00416.97
8.0.60.0050.00316.94
8.0.50.0030.00516.84
8.0.30.0130.00817.31
8.0.20.0090.01417.40
8.0.10.0030.00616.96
8.0.00.0100.01016.69
7.4.330.0000.00515.08
7.4.320.0000.00616.63
7.4.300.0060.00016.52
7.4.290.0040.00416.52
7.4.280.0040.00416.51
7.4.270.0070.00016.68
7.4.260.0000.00716.63
7.4.250.0020.00516.46
7.4.240.0020.00516.44
7.4.230.0040.00416.74
7.4.220.0060.01516.66
7.4.210.0040.01216.65
7.4.200.0000.00816.43
7.4.160.0070.01016.41
7.4.150.0120.00917.40
7.4.140.0170.00917.86
7.4.130.0150.00616.60
7.4.120.0070.01016.44
7.4.110.0170.01116.63
7.4.100.0130.00616.73
7.4.90.0120.00816.53
7.4.80.0140.00819.39
7.4.70.0130.00416.75
7.4.60.0090.00816.45
7.4.50.0030.00616.45
7.4.40.0150.00316.41
7.4.30.0070.01116.57
7.4.00.0000.01715.11
7.3.330.0060.00013.35
7.3.320.0000.00613.36
7.3.310.0000.00716.52
7.3.300.0030.00316.42
7.3.290.0120.00716.41
7.3.280.0100.00516.45
7.3.270.0070.01117.40
7.3.260.0190.00016.60
7.3.250.0100.01016.47
7.3.240.0060.01216.43
7.3.230.0070.01116.40
7.3.210.0080.01116.45
7.3.200.0110.00816.63
7.3.190.0030.01416.29
7.3.180.0050.01316.59
7.3.170.0110.00716.68
7.3.160.0070.01116.62
7.2.330.0130.00916.80
7.2.320.0110.00716.55
7.2.310.0070.01516.93
7.2.300.0090.00916.52
7.2.290.0110.00816.60
7.2.00.0000.01319.36
7.1.100.0060.00618.31
7.1.70.0000.00917.09
7.1.60.0150.01119.48
7.1.50.0110.01116.97
7.1.00.0100.06022.57
7.0.200.0040.00416.85
7.0.60.0100.03320.08
7.0.50.0030.05017.88
7.0.40.0070.04320.30
7.0.30.0170.06720.23
7.0.20.0230.04720.33
7.0.10.0100.06320.21
7.0.00.0030.05320.09
5.6.280.0000.03321.00
5.6.210.0100.07720.58
5.6.200.0070.08318.18
5.6.190.0070.05720.69
5.6.180.0300.07320.77
5.6.170.0070.05020.48
5.6.160.0070.07320.48
5.6.150.0070.08018.19
5.6.140.0030.04718.15
5.6.130.0030.05018.14
5.6.120.0070.04021.14
5.6.110.0030.04321.02
5.6.100.0100.08320.98
5.6.90.0170.07721.14
5.6.80.0200.06720.52
5.5.350.4030.02720.30
5.5.340.0100.05717.94
5.5.330.0070.05020.22
5.5.320.0270.08020.24
5.5.310.0330.07720.30
5.5.300.0100.07317.98
5.5.290.0100.06317.98
5.5.280.0070.08720.83
5.5.270.0030.06020.66
5.5.260.0070.04020.96
5.5.250.0170.07720.63
5.5.240.0270.07020.27

preferences:
30.03 ms | 401 KiB | 5 Q