Converts a nested data array into a flattened structure with a combined key.
This function can be used by translators to help with the data conversion.
Nested keys will be joined together using a colon, so for example $data['key1']['key2']['key3'] will be converted into $flattened_data['key1][key2][key3'].
array $data: The nested array structure that should be flattened.
string $prefix: Internal use only, indicates the current key prefix when recursing into the data array.
array $label: Label for the data.
array The flattened data array.
public function flatten(array $data, $prefix = NULL, $label = array()) {
$flattened_data = array();
if (isset($data['#label'])) {
$label[] = $data['#label'];
}
// Each element is either a text (has #text property defined) or has children,
// not both.
if (!empty($data['#text']) || !empty($data['#file'])) {
$flattened_data[$prefix] = $data;
$flattened_data[$prefix]['#parent_label'] = $label;
}
else {
$prefix = isset($prefix) ? $prefix . static::TMGMT_ARRAY_DELIMITER : '';
foreach (Element::children($data) as $key) {
$flattened_data += $this
->flatten($data[$key], $prefix . $key, $label);
}
}
return $flattened_data;
}