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('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.3.0 - 8.3.6
Array ( [id] => 1 [children] => Array ( [0] => Array ( [id] => 2 [children] => Array ( [id] => 4 [children] => ) ) [1] => Array ( [id] => 3 [children] => ) ) ) Warning: Trying to access array offset on int in /in/FcpdH on line 16 Warning: Trying to access array offset on int in /in/FcpdH on line 16 Warning: Trying to access array offset on int in /in/FcpdH on line 16 Warning: Trying to access array offset on int in /in/FcpdH on line 16 Warning: Trying to access array offset on int in /in/FcpdH on line 16 Warning: Trying to access array offset on int in /in/FcpdH on line 16 Warning: Trying to access array offset on int in /in/FcpdH on line 19 Warning: Trying to access array offset on int in /in/FcpdH on line 16 Warning: Trying to access array offset on false in /in/FcpdH on line 16 Warning: Trying to access array offset on int in /in/FcpdH on line 16 Warning: Trying to access array offset on false in /in/FcpdH on line 16 Warning: Trying to access array offset on false in /in/FcpdH on line 19 Warning: Trying to access array offset on int in /in/FcpdH on line 16 Warning: Trying to access array offset on int in /in/FcpdH on line 16 Warning: Undefined variable $paths in /in/FcpdH on line 10
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18
Array ( [id] => 1 [children] => Array ( [0] => Array ( [id] => 2 [children] => Array ( [id] => 4 [children] => ) ) [1] => Array ( [id] => 3 [children] => ) ) ) Warning: Trying to access array offset on value of type int in /in/FcpdH on line 16 Warning: Trying to access array offset on value of type int in /in/FcpdH on line 16 Warning: Trying to access array offset on value of type int in /in/FcpdH on line 16 Warning: Trying to access array offset on value of type int in /in/FcpdH on line 16 Warning: Trying to access array offset on value of type int in /in/FcpdH on line 16 Warning: Trying to access array offset on value of type int in /in/FcpdH on line 16 Warning: Trying to access array offset on value of type int in /in/FcpdH on line 19 Warning: Trying to access array offset on value of type int in /in/FcpdH on line 16 Warning: Trying to access array offset on value of type bool in /in/FcpdH on line 16 Warning: Trying to access array offset on value of type int in /in/FcpdH on line 16 Warning: Trying to access array offset on value of type bool in /in/FcpdH on line 16 Warning: Trying to access array offset on value of type bool in /in/FcpdH on line 19 Warning: Trying to access array offset on value of type int in /in/FcpdH on line 16 Warning: Trying to access array offset on value of type int in /in/FcpdH on line 16 Warning: Undefined variable $paths in /in/FcpdH on line 10
Output for 7.4.0 - 7.4.25, 7.4.27 - 7.4.33
Array ( [id] => 1 [children] => Array ( [0] => Array ( [id] => 2 [children] => Array ( [id] => 4 [children] => ) ) [1] => Array ( [id] => 3 [children] => ) ) ) Notice: Trying to access array offset on value of type int in /in/FcpdH on line 16 Notice: Trying to access array offset on value of type int in /in/FcpdH on line 16 Notice: Trying to access array offset on value of type int in /in/FcpdH on line 16 Notice: Trying to access array offset on value of type int in /in/FcpdH on line 16 Notice: Trying to access array offset on value of type int in /in/FcpdH on line 16 Notice: Trying to access array offset on value of type int in /in/FcpdH on line 16 Notice: Trying to access array offset on value of type int in /in/FcpdH on line 19 Notice: Trying to access array offset on value of type int in /in/FcpdH on line 16 Notice: Trying to access array offset on value of type bool in /in/FcpdH on line 16 Notice: Trying to access array offset on value of type int in /in/FcpdH on line 16 Notice: Trying to access array offset on value of type bool in /in/FcpdH on line 16 Notice: Trying to access array offset on value of type bool in /in/FcpdH on line 19 Notice: Trying to access array offset on value of type int in /in/FcpdH on line 16 Notice: Trying to access array offset on value of type int in /in/FcpdH on line 16 Notice: Undefined variable: paths in /in/FcpdH on line 10
Output for 7.3.32 - 7.3.33, 7.4.26
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, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.36, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31
Array ( [id] => 1 [children] => Array ( [0] => Array ( [id] => 2 [children] => Array ( [id] => 4 [children] => ) ) [1] => Array ( [id] => 3 [children] => ) ) ) Notice: Undefined variable: paths in /in/FcpdH on line 10
Output for 5.0.0 - 5.0.3
Array ( [id] => 1 [children] => Array ( [0] => Array ( [id] => 2 [children] => Array ( [id] => 4 [children] => ) ) [1] => Array ( [id] => 3 [children] => ) ) ) Notice: Undefined variable: paths in /in/FcpdH 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/FcpdH 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/FcpdH 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/FcpdH on line 3
Process exited with code 255.

preferences:
237.38 ms | 401 KiB | 362 Q