function drush_key_value_to_array_table

8.0.x output.inc drush_key_value_to_array_table($keyvalue_table, $metadata = array())
6.x output.inc drush_key_value_to_array_table($keyvalue_table, $metadata = array())
7.x output.inc drush_key_value_to_array_table($keyvalue_table, $metadata = array())
3.x drush.inc drush_key_value_to_array_table($keyvalue_table)
4.x drush.inc drush_key_value_to_array_table($keyvalue_table)
5.x output.inc drush_key_value_to_array_table($keyvalue_table)
master output.inc drush_key_value_to_array_table($keyvalue_table, $metadata = array())

Convert an associative array of key : value pairs into a table suitable for processing by drush_print_table.

Parameters

$keyvalue_table: An associative array of key : value pairs.

$metadata: 'key-value-item': If the value is an array, then the item key determines which item from the value will appear in the output.

Return value

An array of arrays, where the keys from the input array are stored in the first column, and the values are stored in the third. A second colum is created specifically to hold the ':' separator.

Related topics

2 calls to drush_key_value_to_array_table()
drush_outputformat_key_value::format in commands/core/outputformat/key_value.inc
drush_xkcd_display in examples/xkcd.drush.inc
Display a given XKCD cartoon.

File

includes/output.inc, line 323

Code

function drush_key_value_to_array_table($keyvalue_table, $metadata = array()) {
  if (!is_array($keyvalue_table)) {
    return drush_set_error('DRUSH_INVALID_FORMAT', dt("Data not compatible with selected key-value output format."));
  }
  if (!is_array($metadata)) {
    $metadata = array('key-value-item' => $metadata);
  }
  $item_key = array_key_exists('key-value-item', $metadata) ? $metadata['key-value-item'] : NULL;
  $metadata += array(
    'format' => 'list',
    'separator' => ' ',
  );
  $table = array();
  foreach ($keyvalue_table as $key => $value) {
    if (isset($value)) {
      if (is_array($value) && isset($item_key)) {
        $value = $value[$item_key];
      }
      // We should only have simple values here, but if
      // we don't, use drush_format() to flatten as a fallback.
      if (is_array($value)) {
        $value = drush_format($value, $metadata, 'list');
      }
    }
    if (isset($metadata['include-field-labels']) && !$metadata['include-field-labels']) {
      $table[] = array(isset($value) ? $value : '');
    }
    elseif (isset($value)) {
      $table[] = array($key, ' :', $value);
    }
    else {
      $table[] = array($key . ':', '', '');
    }
  }
  return $table;
}