Skip to content

Commit

Permalink
fix: Optimized array helper method
Browse files Browse the repository at this point in the history
  • Loading branch information
deluxetom committed Jan 15, 2024
1 parent 7a7f93c commit f996d0b
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions src/Core/Helper/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
namespace Secretary\Helper;

/**
* Class ArrayHelper.
*
* @package Secretary\Helper
*/
abstract class ArrayHelper
Expand All @@ -33,22 +31,18 @@ public static function without(array $array, ...$keys): array
}

/**
* Remove elements from an array based on a list of keys and return a new array with the removed elements.
*
* @param string ...$keys
*/
public static function remove(array &$array, ...$keys): array
public static function remove(array $array, ...$keys): array
{
$newArray = [];
foreach (array_keys($newArray) as $key) {
if (!in_array($key, $keys)) {
$newArray[$key] = $array[$key];
unset($array[$key]);
}
}
// Create a copy of the array to avoid modifying the original array
$newArray = $array;

// Iterate over the keys and remove them from the new array
foreach ($keys as $key) {
if (!array_key_exists($key, $newArray)) {
$newArray[$key] = null;
}
unset($newArray[$key]);
}

return $newArray;
Expand Down

0 comments on commit f996d0b

Please sign in to comment.