public static function flat(array $array, $preserve_keys = true)
{
$flattened = array();
array_walk_recursive($array, function ($value, $key) use(&$flattened, $preserve_keys) {
if ($preserve_keys && !is_int($key)) {
$flattened[$key] = $value;
} else {
$flattened[] = $value;
}
});
return $flattened;
}
public function testFlatten() { $input = array('a', 'b', 'c', 'd', array('first' => 'e', 'f', 'second' => 'g', array('h', 'third' => 'i', array(array(array(array('j', 'k', 'l'))))))); $expectNoKeys = range('a', 'l'); $expectWithKeys = array('a', 'b', 'c', 'd', 'first' => 'e', 'f', 'second' => 'g', 'h', 'third' => 'i', 'j', 'k', 'l'); is($expectWithKeys, Arr::flat($input)); is($expectNoKeys, Arr::flat($input, false)); is($expectWithKeys, Arr::flat($input, true)); }