diff --git a/examples/grouping.php b/examples/grouping.php new file mode 100644 index 0000000..0076315 --- /dev/null +++ b/examples/grouping.php @@ -0,0 +1,37 @@ +formatTXT(true); + +// Group by +$result = $table->groupBy([0], function($key, $group){ + $sum = 0; + foreach($group as $row){ + $sum += $row[1]; + } + return [$key, $sum]; +}, true); + +echo "\n"; +$groupBy = new matrix(array_values($result)); +$groupBy->addRow(["Product", "Total"], onTop: true); +echo $groupBy->formatTXT(true); + diff --git a/src/matrix.php b/src/matrix.php index 9a3ef7f..c98a392 100644 --- a/src/matrix.php +++ b/src/matrix.php @@ -88,10 +88,6 @@ public function __construct(array $matrix) */ public function validateMatrix(array $matrix): void { - if (empty($matrix) || !is_array($matrix[0]) || empty($matrix[0])) { - throw new InvalidArgumentException('Matrix must have at least one row with string headers'); - } - $numColumns = count($matrix[0]); foreach ($matrix as $row) { if (!is_array($row) || count($row) !== $numColumns) {