3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Provides a flat list of all the nodes * Eases with modifying the tree inheritance by id */ class NodeList { public $length = 0; /** * @param mixed $index * @return Node|null */ public function item($index) { $tmp = (array) $this; $this->length = count($tmp) - 1; if (false === isset($this->$index)) { return null; } return $this->$index; } } /** * Establish and maintain the tree view of each node/child */ class Tree extends NodeList { /** * Requires id, parent from record set * @param null|array|object $recordSet */ public function __construct($recordSet = null) { if (null !== $recordSet) { $this->setChildren($recordSet); } } /** * @param array|object $recordSet */ private function setChildren($recordSet) { foreach ($recordSet as $record) { if (true === is_array($record)) { $record = (object) $record; } if (true === isset($record->id)) { $this->length++; $this->appendNode($record->id, $record); } } foreach ($this as $node) { if (false === $node instanceof Node) { continue; } if (false === empty($node->parent) && true === isset($this->{$node->parent})) { $children = &$this->{$node->parent}->children; $children->length++; $children->{$node->id} = &$this->{$node->id}; $this->item($node->id); } } } /** * @param string $id * @param null|array|object $data * @return mixed */ public function appendNode($id, $data = null) { $this->$id = new Node($data); return $this->item($id); } /** * @param string $id * @return \Node */ public function getNode($id) { $item = $this->item($id); if (true === empty($item)) { $this->appendNode($id, null); } return $item; } /** * @param string $id * @return \Node|null */ public function getParent($id) { if (null === $this->getNode($id)->parent) { return null; } return $this->getNode($this->getNode($id)->parent); } /** * @param string $id * @return int */ public function getDepth($id) { $i = 0; $item = $this->item($id); if (null !== $item && null !== $item->parent) { $i = $this->getDepth($item->parent) + 1; } $item->depth = $i; return $item->depth; } /** * @param string $id */ public function removeNode($id) { $this->removeChildren($id); if (null !== $this->item(id)) { $parent = false; if ($this->item($id)->parent) { $parent = $this->getParent($id); } $this->$id = null; if ($parent && $parent->children) { unset($parent->children->$id); $parent->children->length--; } } } } /** * Single Node * This is an overloaded class */ class Node { public $id; public $name; public $parent; public $depth; public $children; /** * @param array|object $data */ public function __construct($data) { $this->children = new NodeList(); if (null === $data) { return; } foreach ($data as $key => $value) { /* I escaped these values since they are redundant to this script */ switch ($key) { case 'children': case 'depth': case 'childrenCount': continue 2; } $this->$key = $value; } } /** * @return int */ public function countChildren() { return $this->children->length; } /** * @return \NodeList */ public function getChildren() { return $this->children; } public function removeChildren() { if (null !== $this->getChildren()) { foreach ($this->children as $child) { if (true === isset($child->children)) { $child->removeChildren(); } $child = null; } } $this->children = null; return $this; } } $recordSet = array( array( 'id' => 1, 'name' => 'somename1', 'parent' => null, 'childrenCount' => 0, 'children' => 0, 'foo' => 'foo', ), array( 'id' => 53, 'name' => 'somename2', 'parent' => 1, 'childrenCount' => 0, 'children' => 0, 'bar' => 'bar', ), array( 'id' => 921, 'name' => 'somename3', 'parent' => 53, 'childrenCount' => 0, 'children' => 0, 'baz' => 'baz', ), ); $TreeObject = new Tree($recordSet); echo 'Display the entire tree (This is a fully recursive tree that can be looked up at any point)' . PHP_EOL; print_r($TreeObject); echo PHP_EOL; echo 'Get the middle node id 53' . PHP_EOL; print_r($TreeObject->getNode('53')); echo PHP_EOL . PHP_EOL; echo 'Get the child count of node id 53: ' . $TreeObject->getNode('53')->countChildren(); echo PHP_EOL . PHP_EOL; echo 'Get the children of node 1: ' . PHP_EOL; print_r($TreeObject->getNode('1')->getChildren()); echo PHP_EOL; echo 'Get the depth of the tree node id 921: ' . $TreeObject->getDepth('921'); echo PHP_EOL . PHP_EOL; echo 'Get the depth of the tree node id 53: ' . $TreeObject->getDepth('53'); echo PHP_EOL . PHP_EOL; echo 'Remove the children of node id 53:' . PHP_EOL; print_r($TreeObject->getNode('53')->removeChildren()); echo PHP_EOL; echo 'Display the list of nodes after removing the children of node id 53:' . PHP_EOL; print_r($TreeObject);

Abusive script

This script was stopped while abusing our resources

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.110.0130.00920.94
8.3.100.0060.00924.06
8.3.90.0090.00626.77
8.3.80.0040.00417.00
8.3.70.0120.00618.43
8.3.60.0090.00616.75
8.3.50.0100.01124.43
8.3.40.0040.01218.92
8.3.30.0060.00918.79
8.3.20.0000.00724.18
8.3.10.0040.00424.66
8.3.00.0040.00426.16
8.2.230.0040.00422.58
8.2.220.0060.00337.54
8.2.210.0040.01526.77
8.2.200.0060.00316.75
8.2.190.0110.00716.58
8.2.180.0070.00725.92
8.2.170.0100.00618.95
8.2.160.0150.00022.96
8.2.150.0000.00925.66
8.2.140.0000.00824.66
8.2.130.0050.00326.16
8.2.120.0080.00019.84
8.2.110.0070.00322.07
8.2.100.0000.01218.03
8.2.90.0030.00519.21
8.2.80.0040.00417.97
8.2.70.0030.00517.75
8.2.60.0040.00417.63
8.2.50.0040.00418.07
8.2.40.0030.00617.75
8.2.30.0000.00818.07
8.2.20.0040.00418.20
8.2.10.0030.00619.36
8.2.00.0040.00419.25
8.1.290.0090.00018.88
8.1.280.0040.01125.92
8.1.270.0080.00023.69
8.1.260.0040.00426.35
8.1.250.0050.00328.09
8.1.240.0030.00623.80
8.1.230.0040.00717.96
8.1.220.0080.00017.91
8.1.210.0060.00318.77
8.1.200.0030.00617.60
8.1.190.0000.00817.35
8.1.180.0080.00018.10
8.1.170.0060.00318.66
8.1.160.0050.00218.87
8.1.150.0070.00418.86
8.1.140.0070.00018.82
8.1.130.0020.00517.52
8.1.120.0040.00417.59
8.1.110.0040.00417.52
8.1.100.0050.00217.49
8.1.90.0080.00017.43
8.1.80.0030.00617.55
8.1.70.0070.00017.59
8.1.60.0000.00817.65
8.1.50.0040.00417.56
8.1.40.0040.00417.57
8.1.30.0000.00817.68
8.1.20.0040.00417.63
8.1.10.0000.00817.56
8.1.00.0060.00617.56
8.0.300.0000.00818.77
8.0.290.0090.00016.88
8.0.280.0030.00318.44
8.0.270.0030.00317.98
8.0.260.0030.00316.89
8.0.250.0000.00816.92
8.0.240.0040.00417.07
8.0.230.0040.00417.02
8.0.220.0050.00316.98
8.0.210.0070.00016.82
8.0.200.0050.00316.91
8.0.190.0000.00816.93
8.0.180.0000.00716.97
8.0.170.0030.00616.86
8.0.160.0040.00416.95
8.0.150.0000.00816.88
8.0.140.0070.00316.91
8.0.130.0000.00613.37
8.0.120.0000.00816.86
8.0.110.0050.00216.95
8.0.100.0040.00416.73
8.0.90.0040.00416.84
8.0.80.0060.00816.88
8.0.70.0040.00416.91
8.0.60.0040.00416.82
8.0.50.0000.00716.85
8.0.30.0100.00717.15
8.0.20.0070.01517.04
8.0.10.0070.01117.04
8.0.00.0090.01217.01
7.4.330.0060.00016.84
7.4.320.0000.00616.57
7.4.300.0030.00316.59
7.4.290.0000.00916.55
7.4.280.0060.00316.63
7.4.270.0000.00816.50
7.4.260.0030.00413.24
7.4.250.0040.00416.57
7.4.240.0040.00416.64
7.4.230.0000.00716.47
7.4.220.0000.00716.76
7.4.210.0130.00616.75
7.4.200.0070.00016.79
7.4.150.0100.01016.41
7.4.140.0110.01116.64
7.4.130.0130.01316.61
7.4.120.0070.01716.46
7.4.110.0150.01016.53
7.4.100.0080.01216.40
7.4.90.0000.02016.70
7.4.80.0070.01116.64
7.4.70.0140.00716.48
7.4.60.0140.00716.53
7.4.50.0090.00916.60
7.4.40.0170.00316.63
7.4.30.0180.00316.50
7.4.20.0090.01316.59
7.4.10.0170.00816.68
7.4.00.0160.00316.61
7.3.330.0030.00616.33
7.3.320.0050.00313.45
7.3.310.0040.00416.30
7.3.300.0000.00616.32
7.3.290.0110.00816.44
7.3.270.0090.00916.72
7.3.260.0140.00716.49
7.3.250.0110.01116.61
7.3.240.0120.01216.70
7.3.230.0140.01016.64
7.3.220.0060.01216.41
7.3.210.0090.00916.45
7.3.200.0210.00316.75
7.3.190.0150.00416.55
7.3.180.0120.00816.62
7.3.170.0220.00416.36
7.3.160.0090.00916.38
7.3.150.0170.00316.47
7.3.140.0130.01016.34
7.3.130.0060.01316.41
7.3.120.0120.00616.63
7.3.110.0100.00716.42
7.3.100.0120.00616.52
7.3.90.0170.00316.51
7.3.80.0090.00916.39
7.3.70.0090.00916.44
7.3.60.0120.01016.49
7.3.50.0110.00716.38
7.3.40.0090.00916.46
7.3.30.0080.01216.28
7.3.20.0120.00618.28
7.3.10.0160.00318.15
7.3.00.0080.01218.23
7.2.340.0190.01516.74
7.2.330.0270.00916.66
7.2.320.0240.00816.88
7.2.310.0230.01016.85
7.2.300.0220.01716.86
7.2.290.0250.00916.81
7.2.280.0280.00616.66
7.2.270.0310.00316.72
7.2.260.0230.01316.77
7.2.250.0210.01216.70
7.2.240.0240.01216.93
7.2.230.0250.00916.79
7.2.220.0300.00416.91
7.2.210.0210.01316.60
7.2.200.0240.01116.66
7.2.190.0210.01216.81
7.2.180.0150.01916.64
7.2.170.0210.01116.75
7.2.160.0220.01316.83
7.2.150.0290.00318.55
7.2.140.0140.01418.57
7.2.130.0210.00918.46
7.2.120.0230.01118.37
7.2.110.0180.01118.48
7.2.100.0200.01518.44
7.2.90.0220.01318.20
7.2.80.0240.01018.46
7.2.70.0190.01118.41
7.2.60.0220.01318.25
7.2.50.0190.01418.56
7.2.40.0220.00918.46
7.2.30.0210.01318.37
7.2.20.0240.01018.47
7.2.10.0210.01218.39
7.2.00.0180.01518.24
7.1.330.0220.00817.35
7.1.320.0170.01417.39
7.1.310.0290.00517.47
7.1.300.0200.01017.54
7.1.290.0210.00917.47
7.1.280.0240.00717.56
7.1.270.0160.01617.67
7.1.260.0250.00317.20
7.1.250.0190.01117.39
7.1.240.0250.00417.29
7.1.230.0210.00917.41
7.1.220.0320.00617.29
7.1.210.0230.00917.43
7.1.200.0170.01417.36
7.1.190.0220.00717.20
7.1.180.0240.00717.36
7.1.170.0190.01117.36
7.1.160.0130.01617.51
7.1.150.0240.00717.23
7.1.140.0210.00917.36
7.1.130.0250.00617.51
7.1.120.0180.01517.47
7.1.110.0190.01917.54
7.1.100.0160.01617.29
7.1.90.0110.01517.55
7.1.80.0280.00517.28
7.1.70.0180.01117.55
7.1.60.0190.01017.51
7.1.50.0180.01217.34
7.1.40.0290.00317.35
7.1.30.0130.01817.29
7.1.20.0300.00017.35
7.1.10.0190.01217.35
7.1.00.0270.00417.21
7.0.330.0260.02216.93
7.0.320.0330.00617.29
7.0.310.0340.00816.95
7.0.300.0330.01017.00
7.0.290.0530.01217.07
7.0.280.0250.01617.01
7.0.270.0270.02016.95
7.0.260.0400.01017.12
7.0.250.0350.01017.18
7.0.240.0460.00717.05
7.0.230.0380.00317.24
7.0.220.0290.00817.25
7.0.210.0490.00717.01
7.0.200.0170.02017.03
7.0.190.0280.01017.11
7.0.180.0280.01716.97
7.0.170.0450.00917.06
7.0.160.0290.01117.11
7.0.150.0210.01817.11
7.0.140.0240.01817.05
7.0.130.0280.01117.12
7.0.120.0270.01217.06
7.0.110.0390.01417.12
7.0.100.0230.02217.07
7.0.90.0270.01017.05
7.0.80.0490.00017.19
7.0.70.0450.01117.12
7.0.60.0260.01116.96
7.0.50.0400.00917.01
7.0.40.0370.01217.02
7.0.30.0330.00717.05
7.0.20.0260.01117.07
7.0.10.0230.01616.92
7.0.00.0310.00917.02
5.6.400.0370.01015.89
5.6.390.0260.02316.08
5.6.380.0190.02216.09
5.6.370.0260.01716.02
5.6.360.0540.00315.98
5.6.350.0340.01416.02
5.6.340.0330.01016.21
5.6.330.0270.01716.19
5.6.320.0220.01916.27
5.6.310.0340.01016.15
5.6.300.0370.01116.23
5.6.290.0350.00616.25
5.6.280.0420.01216.00
5.6.270.0330.01016.25
5.6.260.0270.02216.06
5.6.250.0270.01415.88
5.6.240.0330.01516.21
5.6.230.0400.01416.08
5.6.220.0260.02316.21
5.6.210.0340.00715.91
5.6.200.0240.02116.19
5.6.190.0570.00816.09
5.6.180.0340.01216.11
5.6.170.0340.01816.17
5.6.160.0340.00916.05
5.6.150.0390.01716.28
5.6.140.0350.00716.13
5.6.130.0380.02216.02
5.6.120.0310.02516.06
5.6.110.0330.01016.29
5.6.100.0490.01216.26
5.6.90.0290.02116.09
5.6.80.0170.02516.04
5.6.70.0320.02016.15
5.6.60.0380.00615.85
5.6.50.0250.01616.04
5.6.40.0150.02316.25
5.6.30.0410.01515.90
5.6.20.0280.01416.27
5.6.10.0320.01516.00
5.6.00.0260.01916.20
5.5.380.0290.01315.93
5.5.370.0210.02116.16
5.5.360.0280.01815.92
5.5.350.0400.01115.95
5.5.340.0320.01016.10
5.5.330.0290.01416.00
5.5.320.0310.01316.05
5.5.310.0410.00316.17
5.5.300.0280.02016.14
5.5.290.0250.01715.98
5.5.280.0270.02315.85
5.5.270.0200.02416.26
5.5.260.0360.01716.25
5.5.250.0330.02015.97
5.5.240.0340.01615.98
5.5.230.0320.01016.09
5.5.220.0310.01716.01
5.5.210.0380.01016.00
5.5.200.0300.00916.02
5.5.190.0250.01415.90
5.5.180.0220.02216.24
5.5.170.0200.02115.89
5.5.160.0320.01116.05
5.5.150.0400.00916.11
5.5.140.0230.01716.03
5.5.130.0200.02015.87
5.5.120.0390.00716.00
5.5.110.0410.00815.87
5.5.100.0450.01016.11
5.5.90.0370.00615.97
5.5.80.0290.01015.76
5.5.70.0390.00316.05
5.5.60.0160.02416.15
5.5.50.0310.01716.03
5.5.40.0290.00816.02
5.5.30.0310.00915.85
5.5.20.0280.01616.02
5.5.10.0130.02616.07
5.5.00.0280.01715.76
5.4.450.0190.01613.06
5.4.440.0270.00512.95
5.4.430.0290.01112.64
5.4.420.0300.00412.75
5.4.410.0370.00612.96
5.4.400.0260.01012.88
5.4.390.0260.00712.83
5.4.380.0290.01012.93
5.4.370.0270.00712.81
5.4.360.0160.01712.87
5.4.350.0170.01712.75
5.4.340.0220.01212.71
5.4.330.0350.01412.87
5.4.320.0210.01212.86
5.4.310.0230.01312.87
5.4.300.0180.01612.64
5.4.290.0270.00712.86
5.4.280.0300.00313.00
5.4.270.0260.00712.74
5.4.260.0350.01212.82
5.4.250.0260.01212.84
5.4.240.0220.02212.93
5.4.230.0280.01512.78
5.4.220.0340.01112.88
5.4.210.0220.01112.73
5.4.200.0230.01112.70
5.4.190.0270.00612.66
5.4.180.0340.00712.83
5.4.170.0420.01112.86
5.4.160.0260.00712.74
5.4.150.0450.01212.62
5.4.140.0270.01212.90
5.4.130.0310.00612.99
5.4.120.0280.01212.92
5.4.110.0250.00712.87
5.4.100.0200.01713.01
5.4.90.0390.00912.90
5.4.80.0210.01112.74
5.4.70.0200.01612.84
5.4.60.0210.01212.64
5.4.50.0380.01512.87
5.4.40.0370.01012.70
5.4.30.0280.00912.87
5.4.20.0330.00812.93
5.4.10.0350.00612.70
5.4.00.0290.00412.77
5.3.290.0380.00613.20
5.3.280.0200.01713.27
5.3.270.0310.00813.33
5.3.260.0210.02113.28
5.3.250.0320.00713.21
5.3.240.0270.01813.31
5.3.230.0340.01413.23
5.3.220.0240.02012.95
5.3.210.0470.00912.99
5.3.200.0320.01113.00
5.3.190.0240.01213.17
5.3.180.0190.01613.21
5.3.170.0280.01013.29
5.3.160.0290.00413.33
5.3.150.0470.00313.14
5.3.140.0400.00313.22
5.3.130.0240.01813.21
5.3.120.0210.01113.07
5.3.110.0260.01313.14
5.3.100.0190.02313.23
5.3.90.0190.01513.27
5.3.80.0300.01013.16
5.3.70.0210.01213.16
5.3.60.0260.01013.26
5.3.50.0270.01213.13
5.3.40.0350.00512.99
5.3.30.0360.01113.12
5.3.20.0220.02013.19
5.3.10.0280.01712.85
5.3.00.0280.00712.93
5.2.170.0260.01212.24
5.2.160.0270.01012.09
5.2.150.0300.00612.11
5.2.140.0190.01112.14
5.2.130.0260.00811.92
5.2.120.0260.01712.08
5.2.110.0180.01312.03
5.2.100.0240.00812.18
5.2.90.0320.01312.04
5.2.80.0190.01311.91
5.2.70.0200.01012.10
5.2.60.0260.00712.03
5.2.50.0140.02111.97
5.2.40.0380.01012.02
5.2.30.0210.00911.95
5.2.20.0240.00612.03
5.2.10.0280.00811.78
5.2.00.0270.00311.73
5.1.60.0250.00311.13
5.1.50.0230.00411.05
5.1.40.0260.00911.13
5.1.30.0260.00611.29
5.1.20.0280.00411.29
5.1.10.0350.01111.24
5.1.00.0270.00811.30
5.0.50.0170.01110.21
5.0.40.0180.0119.96
5.0.30.0220.0059.87
5.0.20.0200.00610.06
5.0.10.0120.0159.79
5.0.00.0220.00710.04
4.4.90.0050.0058.70
4.4.80.0080.0038.70
4.4.70.0090.0008.70
4.4.60.0070.0038.70
4.4.50.0060.0038.70
4.4.40.0090.0008.70
4.4.30.0070.0028.70
4.4.20.0110.0008.70
4.4.10.0070.0048.70
4.4.00.0060.0038.70
4.3.110.0000.0098.70
4.3.100.0070.0028.70
4.3.90.0040.0058.70
4.3.80.0050.0068.70
4.3.70.0080.0038.70
4.3.60.0100.0008.70
4.3.50.0090.0008.70
4.3.40.0110.0008.70
4.3.30.0060.0038.70
4.3.20.0040.0048.70
4.3.10.0000.0108.70
4.3.00.0080.0008.70

preferences:
34.47 ms | 403 KiB | 5 Q