Skip to content

Commit f1c7772

Browse files
author
Alaa Sarhan
authored
Merge pull request #4 from AlaaSarhan/issue_3_redundant_empty_array
Do not add redundant empty arrays
2 parents d622c2b + fc34883 commit f1c7772

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Flatten.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ private static function flattenGenerator($var, $separator, $prefix = '', $flags)
4545

4646
if ($flags & self::FLAG_NUMERIC_NOT_FLATTENED) {
4747
list ($values, $var) = self::filterNumericKeysAndGetValues($var);
48-
yield $prefix => $values;
48+
if (!empty($values) || empty($var)) {
49+
yield $prefix => $values;
50+
}
4951
}
5052

5153
$prefix .= (empty($prefix) ? '' : $separator);

test/FlattenTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,26 @@ public function flattenWithFlagsProvidor()
176176
'_multidimensional.digit' => 1,
177177
'_emptyArray' => []
178178
]
179+
],
180+
'NUMERIC_NOT_FLATTENED_PASSIVE' => [
181+
[
182+
'numericOnly' => ['A', 'B', 'C', 'D'],
183+
'mixed' => ['A', 'B', 'digit' => 0],
184+
'multidimensional' => [ 'chars' => [8 => 'C', 9 => 'D', 'digit' => 0], 'digit' => 1],
185+
'emptyArray' => []
186+
],
187+
'.',
188+
'_',
189+
Flatten::FLAG_NUMERIC_NOT_FLATTENED,
190+
[
191+
'_numericOnly' => ['A', 'B', 'C', 'D'],
192+
'_mixed' => ['A', 'B'],
193+
'_mixed.digit' => 0,
194+
'_multidimensional.digit' => 1,
195+
'_multidimensional.chars' => [8 => 'C', 9 => 'D'],
196+
'_multidimensional.chars.digit' => 0,
197+
'_emptyArray' => []
198+
]
179199
]
180200
];
181201
}

0 commit comments

Comments
 (0)