3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class CategoryTree { private $catTree = array(); public function addCategory($category, $parent) { if($parent == null){ if(count(array_column($this->catTree, 'category', $category)) > 0){ echo 'Duplicate category'; } else{ $temp = array("category" => $category,"parent" => $parent); array_push($this->catTree,$temp); } } else{ echo 'Parent: ' . $parent; print_r(count(array_column($this->catTree, 'category'), $parent)); } } public function getChildren($parent) { $that = $this; $keys = array_keys(array_column($this->catTree, 'parent'), $parent); return array_map(function($k) use ($that){return $that->catTree[$k]['category'];}, $keys); } } // For testing purposes (do not submit uncommented): $c = new CategoryTree; $c->addCategory('A', null); $c->addCategory('A', null); $c->addCategory('B', 'A'); $c->addCategory('B', 'D'); $c->addCategory('C', 'A'); echo implode(',', $c->getChildren('A'));
based on v9P4e
Output for 7.0.0 - 7.2.0
Duplicate categoryParent: A Warning: count() expects parameter 2 to be integer, string given in /in/gkUgf on line 19 Parent: D Warning: count() expects parameter 2 to be integer, string given in /in/gkUgf on line 19 Parent: A Warning: count() expects parameter 2 to be integer, string given in /in/gkUgf on line 19
Output for 5.5.24 - 5.6.28
Duplicate categoryParent: A Warning: count() expects parameter 2 to be long, string given in /in/gkUgf on line 19 Parent: D Warning: count() expects parameter 2 to be long, string given in /in/gkUgf on line 19 Parent: A Warning: count() expects parameter 2 to be long, string given in /in/gkUgf on line 19
Output for 5.3.21 - 5.4.45
Fatal error: Call to undefined function array_column() in /in/gkUgf on line 9
Process exited with code 255.