3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class A { private $query; public function &findNode(array $path) { $link = &$this->query; foreach ($path as $element) { $link = &$link[$element]; } return $link; } public function setValues(array $path, array $values) { $node = &$this->findNode($path); $node = array_merge($node ?? [], $values); unset($node); return $this; } public function setValueByPath(array $path, $value): self { $link = &$this->findNode($path); if (is_array($value)) { $link[] = $value; } else { $link = $value; } unset($link); return $this; } public function print() { print_r($this->query); } } $a = new A; $a ->setValues(['c', 'd'], [['test' => 1], ['test' => 2]]) ->setValues(['c', 'd'], [['test' => 3], ['test' => 4]]); $a ->setValueByPath(['a', 'b', 'c', 'd'], ['match' => 100500]) ->setValueByPath(['a', 'b', 'c', 'd'], ['match' => 1000]) ->setValueByPath(['a', 'b', 'c', 'd', 1, 'zzz'], 'lol'); $a->print();
based on SofHF
Output for hhvm-3.18.5 - 3.22.0, 7.1.0 - 7.3.0beta1
Array ( [c] => Array ( [d] => Array ( [0] => Array ( [test] => 1 ) [1] => Array ( [test] => 2 ) [2] => Array ( [test] => 3 ) [3] => Array ( [test] => 4 ) ) ) [a] => Array ( [b] => Array ( [c] => Array ( [d] => Array ( [0] => Array ( [match] => 100500 ) [1] => Array ( [match] => 1000 [zzz] => lol ) ) ) ) ) )
Output for 5.6.0 - 5.6.30
Parse error: syntax error, unexpected '?' in /in/LlOhe on line 20
Process exited with code 255.