content_transpose_array_rows_cols

Definition

content_transpose_array_rows_cols($array)
cck-5--1/content.module, line 951

Description

Manipulate a 2D array to reverse rows and columns.

The default data storage for fields is delta first, column names second. This is sometimes inconvenient for field modules, so this function can be used to present the data in an alternate format.

Parameters

$array The array to be transposed. It must be at least two-dimensional, and the subarrays must all have the same keys or behavior is undefined.

Return value

The transposed array.

Code

<?php
function content_transpose_array_rows_cols($array) {
  $result = array();
  if (is_array($array)) {
    foreach ($array as $key1 => $value1) {
      if (is_array($value1)) {
        foreach ($value1 as $key2 => $value2) {
          if (!isset($result[$key2])) {
            $result[$key2] = array();
          }
          $result[$key2][$key1] = $value2;
        }
      }
    }
  }
  return $result;
}
?>