return $newRows;
}
+ /**
+ * Rotate a matrix, converting from row-oriented array to a column-oriented array.
+ *
+ * @param iterable $rows
+ * Ex: [['a'=>10,'b'=>'11'], ['a'=>20,'b'=>21]]
+ * Formula: [scalar $rowId => [scalar $colId => mixed $value]]
+ * @param bool $unique
+ * Only return unique values.
+ * @return array
+ * Ex: ['a'=>[10,20], 'b'=>[11,21]]
+ * Formula: [scalar $colId => [scalar $rowId => mixed $value]]
+ * Note: In unique mode, the $rowId is not meaningful.
+ */
+ public static function asColumns(iterable $rows, bool $unique = FALSE) {
+ $columns = [];
+ foreach ($rows as $rowKey => $row) {
+ foreach ($row as $columnKey => $value) {
+ if (FALSE === $unique) {
+ $columns[$columnKey][$rowKey] = $value;
+ }
+ elseif (!in_array($value, $columns[$columnKey] ?? [])) {
+ $columns[$columnKey][] = $value;
+ }
+ }
+ }
+ return $columns;
+ }
+
/**
* Rewrite the keys in an array.
*
$r[$last] = $value;
}
+ /**
+ * Move an item in an array-tree (if it exists).
+ *
+ * @param array $values
+ * Data-tree
+ * @param string[] $src
+ * Old path for the existing item
+ * @param string[] $dest
+ * New path
+ * @param bool $cleanup
+ * @return int
+ * Number of items moved (0 or 1).
+ */
+ public static function pathMove(&$values, $src, $dest, $cleanup = FALSE) {
+ if (!static::pathIsset($values, $src)) {
+ return 0;
+ }
+ else {
+ $value = static::pathGet($values, $src);
+ static::pathSet($values, $dest, $value);
+ static::pathUnset($values, $src, $cleanup);
+ return 1;
+ }
+ }
+
/**
* Convert a simple dictionary into separate key+value records.
*