3v4l.org

run code in 300+ PHP versions simultaneously
<?php function flatten_array($array, $preserve_keys = 0, &$out = array()) { # Flatten a multidimensional array to one dimension, optionally preserving keys. # # $array - the array to flatten # $preserve_keys - 0 (default) to not preserve keys, 1 to preserve string keys only, 2 to preserve all keys # $out - internal use argument for recursion foreach($array as $key => $child) if(is_array($child)) $out = flatten_array($child, $preserve_keys, $out); elseif($preserve_keys + is_string($key) > 1) $out[$key] = $child; else $out[] = $child; return $out; } $array = array( 'first' => 1, 'second' => 2, 'query' => array( 'id' => 314, 'descripcion' => 'Test', 'files' => array( array( 'name' => 'Readme.txt', 'path' => '/tmp/43143431431343', ), array( 'name' => 'Picture.jpg', 'path' => '/tmp/98794894594655', ), ), ), 'last' => 99, ); echo "Original:\n"; var_export($array); echo "\n\nFlat:\n"; var_export( flatten_array($array, 1) );

preferences:
38.13 ms | 402 KiB | 5 Q