3v4l.org

run code in 500+ PHP versions simultaneously
<?php class CategoryTree { var $tree; public function CategoryTree() { // First element of the array represents the name of the category, so to iterate children, we have to go from index 1 $tree=$this->tree; $tree["root"] = array(); } private function &getCategoryWithNameInSubtree($name, &$subTreeRoot) { if (count($subTreeRoot) == 0) return false; // There are no branches coming from this root // So, the subtree has some branches to traverse... foreach ($subTreeRoot as $branchName => &$branch) { if ($branchName == $name) { // Search is over - this branch has the specified name return $subTreeRoot[$branchName]; } else { $subTreeSearchResult = $this->getCategoryWithNameInSubtree($name, $branch); if($subTreeSearchResult) { return $subTreeSearchResult; } else { //If we have reached this, it means the name was not found in that branch } } } //We traversed all branches and no name was equal to the specified name return false; } public function &getCategoryWithName($name) { $tree=&$this->tree; return $this->getCategoryWithNameInSubtree("seconda", $tree); } } $c = new CategoryTree(); $c->tree=array("prima" => array("prima-prima" => [], "prima-seconda" => [], "prima-terza" => []), "seconda" => array("seconda-prima" => [], "seconda-seconda" => [], "seconda-terza" => []), "terza" => array("terza-prima" => [], "terza-seconda" => [], "terza-terza" => []), ); $seconda=&$c->getCategoryWithName("seconda"); $seconda[] = "added"; print "cat is: <pre>"; print_r($seconda); print "</pre>"; print "ct is: <pre>"; print_r($c); print "</pre>";
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.21, 8.5.0 - 8.5.6
Notice: Only variable references should be returned by reference in /in/9fF0c on line 15 Notice: Only variable references should be returned by reference in /in/9fF0c on line 15 Notice: Only variable references should be returned by reference in /in/9fF0c on line 15 Notice: Only variable references should be returned by reference in /in/9fF0c on line 33 cat is: <pre>Array ( [seconda-prima] => Array ( ) [seconda-seconda] => Array ( ) [seconda-terza] => Array ( ) [0] => added ) </pre>ct is: <pre>CategoryTree Object ( [tree] => Array ( [prima] => Array ( [prima-prima] => Array ( ) [prima-seconda] => Array ( ) [prima-terza] => Array ( ) ) [seconda] => Array ( [seconda-prima] => Array ( ) [seconda-seconda] => Array ( ) [seconda-terza] => Array ( ) [0] => added ) [terza] => Array ( [terza-prima] => Array ( ) [terza-seconda] => Array ( ) [terza-terza] => Array ( ) ) ) ) </pre>
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.31, 7.4.0 - 7.4.33
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; CategoryTree has a deprecated constructor in /in/9fF0c on line 3 Notice: Only variable references should be returned by reference in /in/9fF0c on line 15 Notice: Only variable references should be returned by reference in /in/9fF0c on line 15 Notice: Only variable references should be returned by reference in /in/9fF0c on line 15 Notice: Only variable references should be returned by reference in /in/9fF0c on line 33 cat is: <pre>Array ( [seconda-prima] => Array ( ) [seconda-seconda] => Array ( ) [seconda-terza] => Array ( ) [0] => added ) </pre>ct is: <pre>CategoryTree Object ( [tree] => Array ( [prima] => Array ( [prima-prima] => Array ( ) [prima-seconda] => Array ( ) [prima-terza] => Array ( ) ) [seconda] => Array ( [seconda-prima] => Array ( ) [seconda-seconda] => Array ( ) [seconda-terza] => Array ( ) [0] => added ) [terza] => Array ( [terza-prima] => Array ( ) [terza-seconda] => Array ( ) [terza-terza] => Array ( ) ) ) ) </pre>
Output for 7.3.32 - 7.3.33
cat is: <pre>Array ( [seconda-prima] => Array ( ) [seconda-seconda] => Array ( ) [seconda-terza] => Array ( ) [0] => added ) </pre>ct is: <pre>CategoryTree Object ( [tree] => Array ( [prima] => Array ( [prima-prima] => Array ( ) [prima-seconda] => Array ( ) [prima-terza] => Array ( ) ) [seconda] => Array ( [seconda-prima] => Array ( ) [seconda-seconda] => Array ( ) [seconda-terza] => Array ( ) [0] => added ) [terza] => Array ( [terza-prima] => Array ( ) [terza-seconda] => Array ( ) [terza-terza] => Array ( ) ) ) ) </pre>

preferences:
89.84 ms | 1875 KiB | 4 Q