/** * recursivley expands a flattened array into a heirarchy of arrays * based on a key separator * @param mixed $iterable * @return array returns the same array */ function expand($iterable, $separator = '.') { $array = []; $keys_to_recurse = []; foreach ($iterable as $key => $value) { $sep_position = strpos($key, $separator); if ($sep_position === false) { $array[$key] = $value; /* copy over the value verbatim */ continue; } $prefix = substr($key, 0, $sep_position); $suffix = substr($key, $sep_position + 1); if (!array_key_exists($prefix, $array)) { $array[$prefix] = []; $keys_to_recurse[] = $prefix; } $array[$prefix][$suffix] = $value; } foreach ($keys_to_recurse as $key) { $array[$key] = array_expand($array[$key], $separator); } return $array; }
public function test_array_expand() { $array = array_expand(['foo.bar' => 'baz']); $this->assertEquals(['foo' => ['bar' => 'baz']], $array); }