3v4l.org

run code in 300+ PHP versions simultaneously
<?php function flatten($array, $prefix = '') { $return = []; foreach ($array as $key => $value) { if (is_array($value)) { $return = array_merge($return, flatten($value, $prefix . $key . '_')); } else { $return[$prefix . $key] = $value; } } return $return; } // proof that it's working on the simple cases var_dump(flatten([ 1, 2, 3 => [4, 5] ])); var_dump(flatten([ [1, 2], [3, 4] ])); // use array_values on the result if you don't care about keys // simple multidimensional array var_dump(flatten([ 'a' => [ 'b' => 'value', ] ])); // proof that it's not overwriting values like all the other solutions var_dump(flatten([ 'a' => [ 'b' => 'value', ], 'c' => [ 'b' => 'anothervalue', ], ]));
Output for git.master, git.master_jit, rfc.property-hooks
array(4) { [0]=> int(1) [1]=> int(2) ["3_0"]=> int(4) ["3_1"]=> int(5) } array(4) { ["0_0"]=> int(1) ["0_1"]=> int(2) ["1_0"]=> int(3) ["1_1"]=> int(4) } array(1) { ["a_b"]=> string(5) "value" } array(2) { ["a_b"]=> string(5) "value" ["c_b"]=> string(12) "anothervalue" }

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:
32.03 ms | 406 KiB | 5 Q