function drush_rows_of_key_value_to_array_table

8.0.x output.inc drush_rows_of_key_value_to_array_table($rows_of_keyvalue_table, $header, $metadata)
6.x output.inc drush_rows_of_key_value_to_array_table($rows_of_keyvalue_table, $header, $metadata)
7.x output.inc drush_rows_of_key_value_to_array_table($rows_of_keyvalue_table, $header, $metadata)
master output.inc drush_rows_of_key_value_to_array_table($rows_of_keyvalue_table, $header, $metadata)

Convert an array of data rows, where each row contains an associative array of key : value pairs, into a table suitable for processing by drush_print_table. The provided $header determines the order that the items will appear in the output. Only data items listed in the header will be placed in the output.

Parameters

$rows_of_keyvalue_table: array( 'row1' => array('col1' => 'data', 'col2' => 'data'), 'row2' => array('col1' => 'data', 'col2' => 'data'), )

$header: array('col1' => 'Column 1 Label', 'col2' => 'Column 2 Label')

$metadata: (optional) An array of special options, all optional:

  • strip-tags: call the strip_tags function on the data before placing it in the table
  • concatenate-columns: an array of:
    • dest-col: array('src-col1', 'src-col2')

    Appends all of the src columns with whatever is in the destination column. Appended columns are separated by newlines.

  • transform-columns: an array of:
    • dest-col: array('from' => 'to'),

    Transforms any occurance of 'from' in 'dest-col' to 'to'.

  • format-cell: Drush output format name to use to format any cell that is an array.
  • process-cell: php function name to use to process any cell that is an array.
  • field-mappings: an array whose keys are some or all of the keys in $header and whose values are the corresponding keys to use when indexing the values of $rows_of_keyvalue_table.

Return value

An array of arrays

Related topics

2 calls to drush_rows_of_key_value_to_array_table()
drush_core_watchdog_show_many in commands/core/watchdog.drush.inc
Print a table of watchdog messages.
drush_outputformat_table::format in commands/core/outputformat/table.inc

File

includes/output.inc, line 548

Code

function drush_rows_of_key_value_to_array_table($rows_of_keyvalue_table, $header, $metadata) {
  if (isset($metadata['hide-empty-fields'])) {
    $header = drush_hide_empty_fields($rows_of_keyvalue_table, $header);
  }
  if (empty($header)) {
    $first = (array) reset($rows_of_keyvalue_table);
    $keys = array_keys($first);
    $header = array_combine($keys, $keys);
  }
  $table = array(array_values($header));
  if (isset($rows_of_keyvalue_table) && is_array($rows_of_keyvalue_table) && !empty($rows_of_keyvalue_table)) {
    foreach ($rows_of_keyvalue_table as $row_index => $row_data) {
      $row_data = (array) $row_data;
      $row = array();
      foreach ($header as $column_key => $column_label) {
        $data = drush_lookup_field_by_path($row_data, $column_key, $metadata['field-mappings']);
        if (array_key_exists('transform-columns', $metadata)) {
          foreach ($metadata['transform-columns'] as $dest_col => $transformations) {
            if ($dest_col == $column_key) {
              $data = str_replace(array_keys($transformations), array_values($transformations), $data);
            }
          }
        }
        if (array_key_exists('concatenate-columns', $metadata)) {
          foreach ($metadata['concatenate-columns'] as $dest_col => $src_cols) {
            if ($dest_col == $column_key) {
              $data = '';
              if (!is_array($src_cols)) {
                $src_cols = array($src_cols);
              }
              foreach ($src_cols as $src) {
                if (array_key_exists($src, $row_data) && !empty($row_data[$src])) {
                  if (!empty($data)) {
                    $data .= "\n";
                  }
                  $data .= $row_data[$src];
                }
              }
            }
          }
        }
        if (array_key_exists('format-cell', $metadata) && is_array($data)) {
          $data = drush_format($data, array(), $metadata['format-cell']);
        }
        if (array_key_exists('process-cell', $metadata) && is_array($data)) {
          $data = $metadata['process-cell']($data, $metadata);
        }
        if (array_key_exists('strip-tags', $metadata)) {
          $data = strip_tags($data);
        }
        $row[] = $data;
      }
      $table[] = $row;
    }
  }
  return $table;
}