3v4l.org

run code in 500+ PHP versions simultaneously
<?php class FlattenArray { private array $mapped = []; public function __construct(array $input) { $this->mapped = array_map(fn ($currentKey) => $this->mapRecursive(explode('_', $currentKey), $input[$currentKey]), array_keys($input)); } private function mapRecursive(array $keys, $value) { if (empty($keys)) return; return array_fill_keys( [array_shift($keys)], $this->mapRecursive($keys, $value) ?? $value ); } public function joinAndFlatten() { return $this->mapped; } } var_dump((new FlattenArray(['a_a' => '2', 'a_b' => '333', 'c_c' => '123']))->joinAndFlatten());
Output for git.master, git.master_jit, rfc.property-hooks
array(3) { [0]=> array(1) { ["a"]=> array(1) { ["a"]=> string(1) "2" } } [1]=> array(1) { ["a"]=> array(1) { ["b"]=> string(3) "333" } } [2]=> array(1) { ["c"]=> array(1) { ["c"]=> string(3) "123" } } }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
52.22 ms | 870 KiB | 4 Q