Skip to content

Commit f996d0b

Browse files
committed
fix: Optimized array helper method
1 parent 7a7f93c commit f996d0b

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

src/Core/Helper/ArrayHelper.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
namespace Secretary\Helper;
1212

1313
/**
14-
* Class ArrayHelper.
15-
*
1614
* @package Secretary\Helper
1715
*/
1816
abstract class ArrayHelper
@@ -33,22 +31,18 @@ public static function without(array $array, ...$keys): array
3331
}
3432

3533
/**
34+
* Remove elements from an array based on a list of keys and return a new array with the removed elements.
35+
*
3636
* @param string ...$keys
3737
*/
38-
public static function remove(array &$array, ...$keys): array
38+
public static function remove(array $array, ...$keys): array
3939
{
40-
$newArray = [];
41-
foreach (array_keys($newArray) as $key) {
42-
if (!in_array($key, $keys)) {
43-
$newArray[$key] = $array[$key];
44-
unset($array[$key]);
45-
}
46-
}
40+
// Create a copy of the array to avoid modifying the original array
41+
$newArray = $array;
4742

43+
// Iterate over the keys and remove them from the new array
4844
foreach ($keys as $key) {
49-
if (!array_key_exists($key, $newArray)) {
50-
$newArray[$key] = null;
51-
}
45+
unset($newArray[$key]);
5246
}
5347

5448
return $newArray;

0 commit comments

Comments
 (0)