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]); } } 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.0060.01616.75
8.3.50.0100.00821.98
8.3.40.0120.00318.75
8.3.30.0120.00918.67
8.3.20.0070.00020.33
8.3.10.0040.00423.78
8.3.00.0040.00419.38
8.2.180.0130.00918.41
8.2.170.0030.01622.96
8.2.160.0060.00921.98
8.2.150.0050.00324.18
8.2.140.0040.00424.66
8.2.130.0090.00317.63
8.2.120.0030.00726.35
8.2.110.0060.00322.20
8.2.100.0060.00617.93
8.2.90.0000.00819.36
8.2.80.0000.00817.97
8.2.70.0000.01217.63
8.2.60.0030.00717.93
8.2.50.0000.00918.07
8.2.40.0040.00419.21
8.2.30.0040.00420.68
8.2.20.0060.00317.73
8.2.10.0080.00018.04
8.2.00.0040.00418.10
8.1.280.0090.00625.92
8.1.270.0090.00922.05
8.1.260.0040.00428.09
8.1.250.0040.00428.09
8.1.240.0100.00022.55
8.1.230.0060.00617.64
8.1.220.0040.00417.74
8.1.210.0050.00318.77
8.1.200.0100.00017.35
8.1.190.0050.00317.23
8.1.180.0040.00418.10
8.1.170.0000.00818.40
8.1.160.0000.00821.92
8.1.150.0040.00418.93
8.1.140.0030.00619.52
8.1.130.0040.00417.57
8.1.120.0000.00717.50
8.1.110.0020.00517.45
8.1.100.0030.00517.49
8.1.90.0060.00317.48
8.1.80.0040.00417.33
8.1.70.0030.00317.42
8.1.60.0050.00317.63
8.1.50.0040.00417.46
8.1.40.0040.00417.48
8.1.30.0030.00617.61
8.1.20.0060.00317.66
8.1.10.0040.00417.62
8.1.00.0050.00217.49
8.0.300.0050.00218.77
8.0.290.0000.00716.88
8.0.280.0040.00418.36
8.0.270.0030.00617.24
8.0.260.0000.00617.14
8.0.250.0040.00417.07
8.0.240.0000.00717.01
8.0.230.0000.00816.99
8.0.220.0030.00616.85
8.0.210.0090.00016.90
8.0.200.0000.00617.00
8.0.190.0080.00017.01
8.0.180.0040.00417.02
8.0.170.0000.00816.99
8.0.160.0000.00816.91
8.0.150.0040.00416.90
8.0.140.0070.00016.76
8.0.130.0000.00613.42
8.0.120.0040.00416.93
8.0.110.0050.00316.98
8.0.100.0070.00016.82
8.0.90.0030.00516.83
8.0.80.0070.00716.93
8.0.70.0030.00516.91
8.0.60.0050.00216.95
8.0.50.0000.00717.01
8.0.30.0060.01216.97
8.0.20.0090.00917.41
8.0.10.0000.00816.92
8.0.00.0040.01516.84
7.4.330.0020.00215.08
7.4.320.0050.00216.51
7.4.300.0030.00316.51
7.4.290.0000.00716.58
7.4.280.0080.00016.54
7.4.270.0000.00816.47
7.4.260.0030.00516.54
7.4.250.0080.00016.43
7.4.240.0050.00216.48
7.4.230.0070.00016.53
7.4.220.0030.01416.63
7.4.210.0060.01016.60
7.4.200.0000.00716.58
7.4.160.0130.00316.48
7.4.150.0160.00317.40
7.4.140.0130.00717.86
7.4.130.0090.00916.76
7.4.120.0080.01016.47
7.4.110.0090.00916.41
7.4.100.0100.00716.55
7.4.90.0100.01016.53
7.4.80.0140.02019.39
7.4.70.0080.00816.46
7.4.60.0160.00016.43
7.4.50.0040.00416.47
7.4.40.0070.01416.46
7.4.30.0140.00316.68
7.4.00.0060.00914.68
7.3.330.0050.00013.29
7.3.320.0000.00513.45
7.3.310.0000.00716.28
7.3.300.0000.00716.39
7.3.290.0100.00816.39
7.3.280.0090.00816.44
7.3.270.0110.00717.40
7.3.260.0080.00816.53
7.3.240.0080.01016.39
7.3.230.0160.00616.46
7.3.210.0060.01516.37
7.3.200.0070.02116.42
7.3.190.0130.00416.66
7.3.180.0030.01316.40
7.3.170.0180.00616.50
7.3.160.0060.01016.63
7.3.10.0120.00616.71
7.3.00.0100.00816.57
7.2.330.0030.01416.84
7.2.320.0090.00816.87
7.2.310.0070.01116.88
7.2.300.0070.01016.87
7.2.290.0140.00916.55
7.2.130.0200.00217.00
7.2.120.0070.00916.93
7.2.110.0100.00616.85
7.2.100.0150.00116.65
7.2.90.0130.00316.96
7.2.80.0120.00616.89
7.2.70.0050.01016.86
7.2.60.0090.00516.66
7.2.50.0100.00316.92
7.2.40.0060.00516.99
7.2.30.0150.00616.97
7.2.20.0100.00216.76
7.2.10.0090.00416.89
7.2.00.0050.00817.75
7.1.250.0150.00815.71
7.1.240.0030.00915.87
7.1.230.0060.00915.73
7.1.220.0130.00015.74
7.1.210.0130.00015.85
7.1.200.0100.00015.93
7.1.190.0070.00715.89
7.1.180.0040.01115.91
7.1.170.0060.00615.93
7.1.160.0070.00315.52
7.1.150.0070.00315.76
7.1.140.0100.00315.88
7.1.130.0060.00615.72
7.1.120.0040.00715.78
7.1.110.0060.00615.96
7.1.100.0030.00717.03
7.1.90.0130.00015.63
7.1.80.0060.00815.93
7.1.70.0050.00516.50
7.1.60.0050.01517.74
7.1.50.0070.01116.27
7.1.40.0070.01015.57
7.1.30.0030.00615.87
7.1.20.0030.01015.79
7.1.10.0030.01015.78
7.1.00.0100.03719.01
7.0.330.0040.01215.23
7.0.320.0080.00315.40
7.0.310.0040.01115.58
7.0.300.0030.00715.21
7.0.290.0060.00915.26
7.0.280.0030.00715.39
7.0.270.0060.00315.54
7.0.260.0090.00315.27
7.0.250.0000.01415.50
7.0.240.0030.00615.34
7.0.230.0000.01115.61
7.0.220.0030.00915.34
7.0.210.0030.01015.36
7.0.200.0040.00416.05
7.0.190.0060.00615.40
7.0.180.0060.00615.32
7.0.170.0030.00615.54
7.0.160.0030.00715.60
7.0.150.0060.00615.16
7.0.140.0000.01115.60
7.0.130.0090.00015.59
7.0.120.0080.00315.33
7.0.110.0000.00915.56
7.0.100.0100.00315.48
7.0.90.0050.04817.70
7.0.80.0210.03817.59
7.0.70.0090.03517.55
7.0.60.0100.03817.69
7.0.50.0180.03118.03
7.0.40.0050.04516.51
7.0.30.0000.02916.79
7.0.20.0100.02216.89
7.0.10.0070.02516.88
7.0.00.0050.02816.75
5.6.380.0070.00714.13
5.6.370.0030.00614.66
5.6.360.0040.00414.34
5.6.350.0070.00714.76
5.6.340.0030.01014.38
5.6.330.0030.01014.36
5.6.320.0030.01014.47
5.6.310.0110.00314.60
5.6.300.0040.00414.14
5.6.290.0000.01413.99
5.6.280.0020.02617.67
5.6.270.0100.00714.03
5.6.260.0030.00614.06
5.6.250.0070.00714.13
5.6.240.0020.03117.56
5.6.230.0070.03217.55
5.6.220.0130.03617.29
5.6.210.0120.03917.30
5.6.200.0050.04517.91
5.6.190.0050.04717.82
5.6.180.0020.04517.96
5.6.170.0050.03217.69
5.6.160.0080.04017.60
5.6.150.0030.03817.57
5.6.140.0070.04017.73
5.6.130.0040.03317.79
5.6.120.0020.02817.84
5.6.110.0000.02917.59
5.6.100.0030.04017.85
5.6.90.0090.02417.55
5.6.80.0070.03917.52
5.6.70.0050.02717.45
5.6.60.0160.03617.22
5.6.50.0060.03517.40
5.6.40.0030.02917.26
5.6.30.0100.03217.37
5.6.20.0120.03717.48
5.6.10.0100.03817.46
5.6.00.0030.03017.41
5.5.380.0020.03115.83
5.5.370.0020.02815.83
5.5.360.0050.04416.01
5.5.350.0040.04515.76
5.5.340.0080.04516.00
5.5.330.0070.02616.07
5.5.320.0030.02715.90
5.5.310.0090.04715.94
5.5.300.0080.03315.95
5.5.290.0050.02515.95
5.5.280.0120.03816.11
5.5.270.0090.02416.10
5.5.260.0050.02516.20
5.5.250.0010.04116.03
5.5.240.0000.03115.80
5.5.230.0090.03015.67
5.5.220.0060.03515.75
5.5.210.0030.05115.76
5.5.200.0020.03715.88
5.5.190.0020.03515.79
5.5.180.0060.02115.57
5.5.170.0030.00710.94
5.5.160.0050.02315.75
5.5.150.0130.03515.62
5.5.140.0020.02615.74
5.5.130.0070.03315.67
5.5.120.0050.03315.66
5.5.110.0080.04315.56
5.5.100.0070.03315.81
5.5.90.0020.03015.31
5.5.80.0060.02315.75
5.5.70.0070.04315.59
5.5.60.0080.01815.65
5.5.50.0030.02315.60
5.5.40.0030.03915.55
5.5.30.0090.01815.63
5.5.20.0070.02515.67
5.5.10.0080.03315.58
5.5.00.0110.04115.58

preferences:
56.69 ms | 401 KiB | 5 Q