You are here

function tmgmt_flatten_data in Translation Management Tool 7

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'].

Parameters

$data: The nested array structure that should be flattened.

$prefix: Internal use only, indicates the current key prefix when recursing into the data array.

Return value

array The flattened data array.

See also

tmgmt_unflatten_data()

14 calls to tmgmt_flatten_data()
TMGMTFileFormatHTML::export in translators/file/tmgmt_file.format.html.inc
Return the file content for the job data.
TMGMTFileformatXLIFF::addItem in translators/file/tmgmt_file.format.xliff.inc
Adds a job item to the xml export.
TMGMTFileTestCase::testXLIFF in translators/file/tmgmt_file.test
Tests import and export for the XLIFF format.
TMGMTI18nStringSourcePluginController::saveTranslation in sources/i18n_string/tmgmt_i18n_string.plugin.inc
Saves a translation.
TMGMTJobItem::addTranslatedData in entity/tmgmt.entity.job_item.inc
Adds translated data to a job item.

... See full list

File

./tmgmt.module, line 1331
Main module file for the Translation Management module.

Code

function tmgmt_flatten_data($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'])) {
    $flattened_data[$prefix] = $data;
    $flattened_data[$prefix]['#parent_label'] = $label;
  }
  else {
    $prefix = isset($prefix) ? $prefix . TMGMT_ARRAY_DELIMITER : '';
    foreach (element_children($data) as $key) {
      $flattened_data += tmgmt_flatten_data($data[$key], $prefix . $key, $label);
    }
  }
  return $flattened_data;
}