brenoroosevelt /
php-array-functions
| 1 | <?php |
||
| 2 | declare(strict_types=1); |
||
| 3 | |||
| 4 | namespace BrenoRoosevelt; |
||
| 5 | |||
| 6 | /** |
||
| 7 | * Remove elements by keys |
||
| 8 | * |
||
| 9 | * @param array $set The collection |
||
| 10 | * @param string|int $key The key to be removed |
||
| 11 | * @param string|int ...$keys The keys to be removed |
||
| 12 | * @return int The number of removed elements |
||
| 13 | */ |
||
| 14 | function remove_key(array &$set, $key, ...$keys): int |
||
| 15 | { |
||
| 16 | $removed = 0; |
||
| 17 | $keys[] = $key; |
||
| 18 | foreach ($keys as $key) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 19 | if (array_key_exists($key, $set)) { |
||
| 20 | unset($set[$key]); |
||
| 21 | $removed++; |
||
| 22 | } |
||
| 23 | } |
||
| 24 | |||
| 25 | return $removed; |
||
| 26 | } |
||
| 27 |