<?php class IndxedArrayWrapper { private object $properties; public function __construct(array $properties) { $this->properties = (object) []; array_walk($properties, fn($key, $value) => $this->__set($key, $value)); } public function __get(string|int $index): mixed { return $this->properties?->{$index} ?? null; } public function __set(string|int $key, mixed $value): void { $this->properties->{count((array) $this->properties)} = ['key' => $key, 'value' => $value]; } } ['key' => $fruit, 'value' => $colour] = (new IndxedArrayWrapper([ 'apple' => 'red', 'bananna' => 'yellow' ]))->{0}; echo sprintf('At position 0 is a %s %s', $fruit, $colour);
You have javascript disabled. You will not be able to edit any code.