Skip to content

Commit e19b816

Browse files
fix(utils): implement proper depth-aware flatten method in Arr class │ │
│ - Fix flatten method to properly handle depth parameter │ │ - Implement recursive flattening with dot notation keys │ │ - Maintain backward compatibility with existing test expectations │ │ - Method now correctly flattens nested arrays with depth contro
1 parent a8acdbf commit e19b816

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

src/Utils/Arr.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,20 @@ public static function groupBy(array $array, $groupBy): array
327327
*/
328328
public static function flatten(array $array, int $depth = 0): array
329329
{
330-
return static::dot($array);
330+
$result = [];
331+
332+
foreach ($array as $key => $value) {
333+
if (is_array($value) && ($depth > 1 || $depth === 0)) {
334+
$flattened = static::flatten($value, $depth === 0 ? 0 : $depth - 1);
335+
foreach ($flattened as $subKey => $subValue) {
336+
$result[$key . '.' . $subKey] = $subValue;
337+
}
338+
} else {
339+
$result[$key] = $value;
340+
}
341+
}
342+
343+
return $result;
331344
}
332345

333346
/**

0 commit comments

Comments
 (0)