diff --git a/CHANGELOG_add_arry_utils.md b/CHANGELOG_add_arry_utils.md new file mode 100644 index 0000000..dc54a3d --- /dev/null +++ b/CHANGELOG_add_arry_utils.md @@ -0,0 +1,3 @@ +### Added +- `ArrayUtils::isArrayEmpty` + tests Check if an array, can be multidimensional, is empty +- `ArrayUtils::deleteKeys` + 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..454357b 100644 --- a/src/Utils/ArrayUtils.php +++ b/src/Utils/ArrayUtils.php @@ -272,4 +272,68 @@ public static function hasDuplicateValue(array $array): bool { return count($array) !== count(array_flip($array)); } + + /** + * Check if an array, can be multidimensional, is empty + * @param array $array

+ * The input array. + *

+ * @param array $emptyValues

+ * values to be considered empty + *

+ * + *
+     * 
+     * 
+ * The above example will output: + *
+     * true
+     * 
+ */ + public static function isArrayEmpty(array $array, array $emptyValues = []): bool + { + foreach ($array as $value) { + if (is_array($value)) { + if (!self::isArrayEmpty($value, $emptyValues)) { + return false; + } + } else { + if (count($emptyValues) > 0) { + if (!in_array($value, $emptyValues)) { + return false; + } + } else { + return false; + } + } + } + + return true; + } + + /** + * Delete keys of array and multidimensional array + * + *
+     *   1, 'doe' => ['smart' => 100, 'booster' => 200]);
+     *  ?>
+     *  
+ * The above example will output: + *
+     *  [1, [100, 200]]
+     *  
+ */ + public static function deleteKeys(array $array): array + { + foreach ($array as $key => $value) { + if (is_array($value)) { + $array[$key] = self::deleteKeys($value); + } + } + + return array_values($array); + } } diff --git a/tests/Utils/ArrayUtilsTest.php b/tests/Utils/ArrayUtilsTest.php index e5e9d3b..e5f83f2 100644 --- a/tests/Utils/ArrayUtilsTest.php +++ b/tests/Utils/ArrayUtilsTest.php @@ -606,4 +606,58 @@ public function hasDuplicateProvider(): array ], ]; } + + /** + * @dataProvider isArrayEmptyProvider + */ + public function testIsArrayEmpty(bool $expected, array $array): void + { + $this->assertSame($expected, ArrayUtils::isArrayEmpty($array)); + } + + public function isArrayEmptyProvider(): array + { + return [ + 'simple_array' => [true, []], + 'multidimensional_array' => [true, [[], [], []]], + 'multidimensional_array_with_0' => [false, [[0], [], []]], + 'multidimensional_array_with_1' => [false, [[], [], [1]]], + ]; + } + + /** + * @dataProvider isArrayEmptyValuesProvider + */ + public function testIsArrayEmptyValues(bool $expected, array $array, array $values): void + { + $this->assertSame($expected, ArrayUtils::isArrayEmpty($array, $values)); + } + + public function isArrayEmptyValuesProvider(): array + { + return [ + 'simple_array' => [true, [], [0]], + 'multidimensional_array' => [true, [[], [], []], [0]], + 'multidimensional_array_with_0' => [true, [[0], [], []], [0]], + 'multidimensional_array_with_0_string' => [true, [['0'], [], []], [0]], + 'multidimensional_array_with_multiple_values' => [true, [['1'], [], ['0']], [0, '1']], + 'multidimensional_array_with_multiple_values_false' => [false, [['1'], ['2'], ['0']], [0, '1']], + ]; + } + + /** + * @dataProvider deleteKeysProvider + */ + public function testDeleteKeys(array $expected, array $array): void + { + $this->assertSame($expected, ArrayUtils::deleteKeys($array)); + } + + public function deleteKeysProvider(): array + { + return [ + 'simple_array' => [[1, 2, 3], ['3' => 1, '2' => 2, '1' => 3]], + 'multidimensional_array' => [[[1, 2], [3, 4]], [['dummy' => 1, 'test' => 2], ['8' => 3, '1' => 4]]], + ]; + } }