3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Sqltree { private $FlatTable, $paths; public function __construct($FlatTable) { $this->FlatTable = $FlatTable; } public function generateTable($tree) { $this->paths = array(); $this->path_recursion($tree, array()); print_r($paths); } private function path_recursion($tree, $parents) { //Path erzeugen $this->paths[] = array($tree, $tree, 0); foreach($parents as $parent) { $this->paths[] = array($parent['id'], $tree['id'], $parents['path_length']); $parents['path_length']++; } if(is_array($tree['children'])) { $parents = array('id' => $tree['id'], 'path_length' => 1); foreach($tree['children'] as $child) { $this->path_recursion($child, $parents); } } } public static function CreateTable($FlatTable) { $sql = "CREATE TABLE ClosureTable ( ancestor_id INT NOT NULL REFERENCES $FlatTable(id), descendant_id INT NOT NULL REFERENCES $FlatTable(id), path_length INT, PRIMARY KEY (ancestor_id, descendant_id) );"; sql::create($sql); } } $tree = array(array('id' => 1, 'children' => array(array('id' => 2, 'children' => array('id' => 4, 'children' => false)), array('id' => 3, 'children' => false)))); print_r($tree); $sqltree = new SqlTree('asd'); $sqltree->generateTable($tree); ?>
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Array ( [0] => Array ( [id] => 1 [children] => Array ( [0] => Array ( [id] => 2 [children] => Array ( [id] => 4 [children] => ) ) [1] => Array ( [id] => 3 [children] => ) ) ) ) Warning: Undefined array key "children" in /in/klltQ on line 19 Warning: Undefined variable $paths in /in/klltQ on line 10
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.25, 7.4.27 - 7.4.33
Array ( [0] => Array ( [id] => 1 [children] => Array ( [0] => Array ( [id] => 2 [children] => Array ( [id] => 4 [children] => ) ) [1] => Array ( [id] => 3 [children] => ) ) ) ) Notice: Undefined index: children in /in/klltQ on line 19 Notice: Undefined variable: paths in /in/klltQ on line 10
Output for 7.3.32 - 7.3.33, 7.4.26
Array ( [0] => Array ( [id] => 1 [children] => Array ( [0] => Array ( [id] => 2 [children] => Array ( [id] => 4 [children] => ) ) [1] => Array ( [id] => 3 [children] => ) ) ) )
Output for 5.0.4 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Array ( [0] => Array ( [id] => 1 [children] => Array ( [0] => Array ( [id] => 2 [children] => Array ( [id] => 4 [children] => ) ) [1] => Array ( [id] => 3 [children] => ) ) ) ) Notice: Undefined index: children in /in/klltQ on line 19 Notice: Undefined variable: paths in /in/klltQ on line 10
Output for 5.0.0 - 5.0.3
Array ( [0] => Array ( [id] => 1 [children] => Array ( [0] => Array ( [id] => 2 [children] => Array ( [id] => 4 [children] => ) ) [1] => Array ( [id] => 3 [children] => ) ) ) ) Notice: Undefined index: children in /in/klltQ on line 19 Notice: Undefined variable: paths in /in/klltQ on line 10
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/klltQ on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/klltQ on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/klltQ on line 3
Process exited with code 255.

preferences:
260.57 ms | 401 KiB | 459 Q