You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
groupBy() return Table-of-Tables with key mode support
groupBy() now collects all rows per group into sub-Tables instead of keeping only the first row. An optional GroupByKeyMode enum parameter controls how group keys are derived (VarExport, Strval, or Int).
Also adds get() to retrieve a row/group by key, toJson() for JSON serialization, and widens type annotations across the class to support the mixed Arr|Table row structure. Updates tests, docs, and examples.
Copy file name to clipboardExpand all lines: README.md
+43-1Lines changed: 43 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -206,10 +206,11 @@ The methods:
206
206
- select(): select some fields
207
207
- except(): exclude some fields
208
208
- where(): filter data
209
-
- groupBy(): grouping data
209
+
- groupBy(): grouping data into a Table of Tables, with an optional key mode (`GroupByKeyMode::VarExport`, `GroupByKeyMode::Strval`, `GroupByKeyMode::Int`)
210
210
- transform(): transforms a specific field with the provided function
211
211
- orderBy(): sorting data (ascending or descending)
212
212
- toArray(): transform Table object into a native PHP array
213
+
- toJson(): convert Table object into a JSON string
213
214
214
215
215
216
`Table` now implements `\Countable` and `\Iterator`, this allows you to count the number of rows
The `groupBy()` method groups rows by a field value and returns a **Table of Tables**. You can then use `get()` to access each group by its key and drill into the rows:
Because `groupBy()` returns a Table, you can chain it with `where()` and `orderBy()` to build powerful queries. For example, to get the cheapest active product in each group:
Copy file name to clipboardExpand all lines: doc/table.md
+55-15Lines changed: 55 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -190,11 +190,10 @@ var_export(
190
190
Both orders can be inforced by using `asc` and `desc`
191
191
192
192
## Group By a column
193
-
For example if you want to group the products by their `product` value you can use *groupBy()* method.
193
+
The `groupBy()` method groups rows by a field value and returns a **Table of Tables**. Each entry in the returned Table is itself a Table containing all rows that share the same field value.
The `groupBy()` method accepts an optional second parameter to control how the group keys are generated. The `GroupByKeyMode` enum provides three options:
240
+
241
+
```php
242
+
use HiFolks\DataType\Enums\GroupByKeyMode;
243
+
```
244
+
245
+
-**`GroupByKeyMode::VarExport`** (default): uses `var_export()` to generate keys. This avoids collisions between values like `true` and `1` or `false` and `0`, because they produce distinct string keys (`'true'` vs `'1'`).
246
+
-**`GroupByKeyMode::Strval`**: uses `strval()` to generate keys. This produces cleaner string keys (e.g. `'Desk'` instead of `"'Desk'"`), but `true` and `1` will collide (both become `"1"`).
247
+
-**`GroupByKeyMode::Int`**: uses `intval()` to generate integer keys. Useful for numeric grouping or when you want to truncate float values (e.g. `1.5` and `1.9` both become key `1`).
248
+
249
+
```php
250
+
// Default (VarExport): keys are "'Desk'", "'Chair'", etc.
0 commit comments