diff --git a/CHANGELOG_add_array_utils.md b/CHANGELOG_add_array_utils.md new file mode 100644 index 0000000..e0548e9 --- /dev/null +++ b/CHANGELOG_add_array_utils.md @@ -0,0 +1,2 @@ +### Added +- `ArrayUtils::toIndexedArray` + tests Delete keys of array and multidimensional array \ No newline at end of file diff --git a/src/Utils/ArrayUtils.php b/src/Utils/ArrayUtils.php index ded0273..d8f510e 100644 --- a/src/Utils/ArrayUtils.php +++ b/src/Utils/ArrayUtils.php @@ -272,4 +272,28 @@ public static function hasDuplicateValue(array $array): bool { return count($array) !== count(array_flip($array)); } + + /** + * Delete keys of array and multidimensional array + * + *
+     *   1, 'doe' => ['smart' => 100, 'booster' => 200]);
+     *  ?>
+     *  
+ * The above example will output: + *
+     *  [1, [100, 200]]
+     *  
+ */ + public static function toIndexedArray(array $array): array + { + foreach ($array as $key => $value) { + if (is_array($value)) { + $array[$key] = self::toIndexedArray($value); + } + } + + return array_values($array); + } } diff --git a/src/Utils/DateUtils.php b/src/Utils/DateUtils.php index 963cc45..c7ed9d8 100644 --- a/src/Utils/DateUtils.php +++ b/src/Utils/DateUtils.php @@ -43,7 +43,7 @@ public static function getMonthChoices(string $locale = 'fr_FR'): array { $formatter = new \IntlDateFormatter(locale: $locale, dateType: \IntlDateFormatter::FULL, timeType: \IntlDateFormatter::FULL, pattern: 'MMMM'); for ($i = 1; $i <= 12; $i++) { - $month = $formatter->format(strtotime("2000-$i")); // @phpstan-ignore-line MDT the year is arbitrary and does not impact the month + $month = $formatter->format(strtotime("2000-$i")); // MDT the year is arbitrary and does not impact the month $toReturn[ucfirst((string) $month)] = $i; } diff --git a/tests/Utils/ArrayUtilsTest.php b/tests/Utils/ArrayUtilsTest.php index e5e9d3b..f1a3082 100644 --- a/tests/Utils/ArrayUtilsTest.php +++ b/tests/Utils/ArrayUtilsTest.php @@ -606,4 +606,22 @@ public function hasDuplicateProvider(): array ], ]; } + + /** + * @dataProvider toIndexedArrayProvider + */ + public function testToIndexedArray(array $expected, array $array): void + { + $this->assertSame($expected, ArrayUtils::toIndexedArray($array)); + } + + public function toIndexedArrayProvider(): array + { + return [ + 'simple_associative_array' => [[1, 2, 3], ['3' => 1, '2' => 2, '1' => 3]], + 'simple_indexed_array_no_chagnes' => [[1, 2, 3], [1, 2, 3]], + 'multidimensional_associative_array' => [[[1, 2], [3, 4]], [['dummy' => 1, 'test' => 2], ['8' => 3, '1' => 4]]], + 'multidimensional_indexed_array_no_changes' => [[[1, 2], [3, 4]], [[1, 2], [3, 4]]], + ]; + } }