3v4l.org

run code in 300+ PHP versions simultaneously
<?php $nest = new class([ "first" => [ "I", "am", "a", "animal" => "beaver" ] ]) implements ArrayAccess { public function __construct(array $initial = []) { foreach ($initial as $k => $v) { if (is_array($v)) { $this->$k = new self($v); } else $this->$k = $v; } } public function offsetGet($offset) { if (!isset($this->$offset)) { return ($this->$offset = new self()); } return $this->$offset; } public function offsetSet($offset, $value) { if (is_array($value)) { $this->$offset = new self($value); } else $this->$offset = $value; } public function offsetUnset($offset) { unset($this->$offset); } public function offsetExists($offset) { return isset($this->$offset); } public function array(self $thing = null) { $result = []; if (!$thing) $thing = $this; foreach ($thing as $k => $v) { if ($v instanceof self) { $result[$k] = $this->array($v); } else $result[$k] = $v; } return $result; } public function __invoke(...$args) {} }; $nest[1][2] = "hello"; # write undefined index $nest[5][8][10] = "whatever"; # write more undef index $nest[9][10](); # read undef index and invoke it var_dump($nest->array()); # show array(s), not needed for iteration and whatever ... ?>

preferences:
52.59 ms | 402 KiB | 5 Q