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 getIterator { 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]); } } var_dump(json_encode($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.0210.00016.75
8.3.50.0040.01118.18
8.3.40.0150.00018.59
8.3.30.0120.00318.83
8.3.20.0000.00820.34
8.3.10.0040.00423.64
8.3.00.0030.00519.51
8.2.180.0140.00718.29
8.2.170.0100.00322.96
8.2.160.0030.01022.18
8.2.150.0040.00424.18
8.2.140.0040.00424.66
8.2.130.0070.00717.50
8.2.120.0090.00026.35
8.2.110.0060.00319.25
8.2.100.0060.00617.97
8.2.90.0000.00817.88
8.2.80.0060.00317.97
8.2.70.0080.00017.68
8.2.60.0040.00417.80
8.2.50.0040.00418.10
8.2.40.0040.00420.47
8.2.30.0000.00720.50
8.2.20.0100.00017.73
8.2.10.0030.00518.16
8.2.00.0000.00717.96
8.1.280.0090.00625.92
8.1.270.0140.00421.83
8.1.260.0080.00028.09
8.1.250.0080.00028.09
8.1.240.0060.00322.14
8.1.230.0110.00019.10
8.1.220.0000.00817.79
8.1.210.0000.00918.77
8.1.200.0060.00617.35
8.1.190.0060.00317.25
8.1.180.0040.00418.10
8.1.170.0060.00318.64
8.1.160.0000.00721.98
8.1.150.0040.00418.76
8.1.140.0030.00519.48
8.1.130.0070.00017.77
8.1.120.0000.00717.36
8.1.110.0030.00517.48
8.1.100.0000.00817.45
8.1.90.0000.00817.45
8.1.80.0000.00717.35
8.1.70.0000.00717.32
8.1.60.0000.00817.46
8.1.50.0000.00917.56
8.1.40.0000.00917.41
8.1.30.0050.00317.48
8.1.20.0080.00017.64
8.1.10.0060.00317.58
8.1.00.0000.00717.55
8.0.300.0040.00421.50
8.0.290.0000.00916.75
8.0.280.0030.00318.44
8.0.270.0050.00317.31
8.0.260.0000.00717.25
8.0.250.0000.00716.96
8.0.240.0000.00717.01
8.0.230.0040.00317.03
8.0.220.0030.00316.97
8.0.210.0000.00716.94
8.0.200.0000.00617.02
8.0.190.0030.00516.88
8.0.180.0020.00516.96
8.0.170.0050.00316.96
8.0.160.0000.00816.99
8.0.150.0050.00216.78
8.0.140.0040.00416.88
8.0.130.0030.00313.36
8.0.120.0080.00016.76
8.0.110.0040.00416.95
8.0.100.0000.00916.94
8.0.90.0000.00716.79
8.0.80.0110.00316.90
8.0.70.0030.00516.96
8.0.60.0040.00416.82
8.0.50.0040.00416.89
8.0.30.0060.01217.10
8.0.20.0120.01017.40
8.0.10.0040.00416.92
8.0.00.0090.01016.64
7.4.330.0030.00315.14
7.4.320.0000.00716.42
7.4.300.0000.00616.49
7.4.290.0030.00316.57
7.4.280.0000.00716.55
7.4.270.0040.00416.64
7.4.260.0040.00316.54
7.4.250.0080.00016.61
7.4.240.0050.00216.55
7.4.230.0000.00716.58
7.4.220.0060.01216.45
7.4.210.0030.01316.53
7.4.200.0000.00716.68
7.4.160.0000.01516.66
7.4.150.0090.00917.40
7.4.140.0070.01317.86
7.4.130.0100.01116.56
7.4.120.0130.00416.66
7.4.110.0090.01016.71
7.4.100.0140.00516.50
7.4.90.0130.00616.43
7.4.80.0070.01019.39
7.4.70.0070.01116.76
7.4.60.0050.01116.59
7.4.50.0040.00416.63
7.4.40.0120.01216.64
7.4.30.0120.00616.54
7.4.00.0040.01314.96
7.3.330.0000.00513.11
7.3.320.0000.00613.26
7.3.310.0040.00416.27
7.3.300.0000.00616.20
7.3.290.0110.00416.21
7.3.280.0090.00616.23
7.3.270.0140.00317.40
7.3.260.0130.00316.48
7.3.250.0130.01016.30
7.3.240.0110.00916.28
7.3.230.0100.00716.44
7.3.210.0120.00816.31
7.3.200.0030.01416.19
7.3.190.0110.00616.19
7.3.180.0080.01116.44
7.3.170.0030.01816.36
7.3.160.0060.01016.54
7.3.120.0090.00914.75
7.3.110.0110.00814.87
7.3.100.0100.00714.87
7.3.90.0040.01114.46
7.3.80.0060.01014.39
7.3.70.0080.00414.53
7.3.60.0110.00714.53
7.3.50.0060.01214.77
7.3.40.0110.00714.58
7.3.30.0100.00714.81
7.3.20.0090.00616.54
7.3.10.0030.01416.64
7.3.00.0000.00916.58
7.2.330.0090.00916.77
7.2.320.0060.01516.69
7.2.310.0150.00316.64
7.2.300.0110.00616.33
7.2.290.0070.01116.43
7.2.250.0040.01215.16
7.2.240.0060.00914.88
7.2.230.0060.00914.80
7.2.220.0030.01315.03
7.2.210.0040.01114.91
7.2.200.0030.01015.03
7.2.190.0060.01215.07
7.2.180.0070.01114.86
7.2.170.0100.00714.94
7.2.60.0040.00416.49
7.2.00.0040.00819.35
7.1.330.0130.00015.64
7.1.320.0070.00715.66
7.1.310.0040.00415.34
7.1.300.0100.00315.41
7.1.290.0070.01015.62
7.1.280.0000.01715.70
7.1.270.0110.00715.42
7.1.260.0030.01015.59
7.1.200.0030.01015.52
7.1.100.0070.00417.99
7.1.70.0000.00716.98
7.1.60.0070.01419.48
7.1.50.0040.01816.48
7.1.00.0030.07722.37
7.0.200.0000.00716.54
7.0.90.0730.08020.07
7.0.80.0030.07720.02
7.0.70.0070.05719.95
7.0.60.0130.07319.98
7.0.50.0070.05020.33
7.0.40.0100.04020.14
7.0.30.0070.08320.17
7.0.20.0070.07020.11
7.0.10.0170.07019.95
7.0.00.0100.08720.02
5.6.280.0000.06021.09
5.6.240.0070.08020.68
5.6.230.0100.05720.61
5.6.220.0130.07020.59
5.6.210.0130.08020.47
5.6.200.0100.07721.13
5.6.190.0100.08021.09
5.6.180.0100.08321.04
5.6.170.0030.08320.97
5.6.160.0170.07021.02
5.6.150.0100.08320.95
5.6.140.0170.07721.03
5.6.130.0070.09021.03
5.6.120.0200.07321.03
5.6.110.0070.08720.93
5.6.100.0000.08721.08
5.6.90.0070.07720.94
5.6.80.0070.08720.33
5.6.70.0030.08720.34
5.6.60.0100.08320.45
5.6.50.0070.07720.57
5.6.40.0070.08320.30
5.6.30.0130.07320.47
5.6.20.0030.07020.38
5.6.10.0000.05020.47
5.6.00.0200.07020.31
5.5.380.0130.07720.46
5.5.370.0030.09020.42
5.5.360.0070.08020.50
5.5.350.0130.07720.45
5.5.340.0030.06720.66
5.5.330.0030.05020.79
5.5.320.0000.07320.89
5.5.310.0130.07720.79
5.5.300.0130.08020.84
5.5.290.0130.07320.79
5.5.280.0070.08320.92
5.5.270.0030.08320.93
5.5.260.0070.09020.92
5.5.250.0130.07720.47
5.5.240.0130.07320.21
5.5.230.0130.07320.30
5.5.220.0100.07020.25
5.5.210.0130.07020.03
5.5.200.0070.07320.28
5.5.190.0130.07720.27
5.5.180.0100.07720.29
5.5.160.0100.07720.22
5.5.150.0170.06020.00
5.5.140.0130.07320.26
5.5.130.0200.06020.28
5.5.120.0130.06720.19
5.5.110.0100.07320.23
5.5.100.0100.07020.10
5.5.90.0070.08020.02
5.5.80.0070.07320.08
5.5.70.0100.07720.07
5.5.60.0100.07020.10
5.5.50.0030.07720.07
5.5.40.0070.08019.96
5.5.30.0100.07020.07
5.5.20.0100.07319.92
5.5.10.0030.08020.14
5.5.00.0130.07320.04

preferences:
54.28 ms | 400 KiB | 5 Q