3v4l.org

run code in 300+ PHP versions simultaneously
<?php class CategoryTree { public $tree; private $failure = false; public function __construct() { // 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 $this->failure; // 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 $this->failure; } public function &getCategoryWithName($name) { $tree = &$this->tree; return $this->getCategoryWithNameInSubtree($name, $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 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
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 ( ) ) [terza] => Array ( [terza-prima] => Array ( ) [terza-seconda] => Array ( ) [terza-terza] => Array ( ) ) ) [failure:CategoryTree:private] => ) </pre>
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 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 ( ) ) [terza] => Array ( [terza-prima] => Array ( ) [terza-seconda] => Array ( ) [terza-terza] => Array ( ) ) ) [failure:CategoryTree:private] => ) </pre>

preferences:
166.56 ms | 405 KiB | 186 Q