Skip to content

Commit 8f7c77a

Browse files
committed
v2.2.0 updates
1 parent 1a1cf4e commit 8f7c77a

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
- `Fixed` for any bug fixes.
1313
- `Security` in case of vulnerabilities
1414

15+
## [2.2.0] - 2025.07.22
16+
17+
### Added
18+
19+
- Added `exceptValues` and `getRandomItems` methods
20+
1521
## [2.1.0] - 2025.07.10
1622

1723
### Added

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ composer require bayfrontmedia/php-array-helpers
3939
- [pluck](#pluck)
4040
- [forget](#forget)
4141
- [except](#except)
42+
- [exceptValues](#exceptvalues)
4243
- [only](#only)
4344
- [missing](#missing)
4445
- [isMissing](#ismissing)
4546
- [multisort](#multisort)
4647
- [numericMultisort](#numericmultisort)
4748
- [renameKeys](#renamekeys)
4849
- [order](#order)
50+
- [getRandomItems](#getrandomitems)
4951
- [query](#query)
5052
- [getAnyValues](#getanyvalues)
5153
- [hasAnyValues](#hasanyvalues)
@@ -306,6 +308,35 @@ $array = Arr::except($array, 'active');
306308

307309
<hr />
308310

311+
### exceptValues
312+
313+
**Description:**
314+
315+
Returns the original array except given value(s).
316+
317+
**Parameters:**
318+
319+
- `$array` (array): Original array
320+
- `$values` (string|array): Values(s) to remove
321+
322+
**Returns:**
323+
324+
- (array)
325+
326+
**Example:**
327+
328+
```
329+
$array = [
330+
'John',
331+
'Jane',
332+
'Bob'
333+
];
334+
335+
$array = Arr::exceptValues($array, 'John');
336+
```
337+
338+
<hr />
339+
309340
### only
310341

311342
**Description:**
@@ -566,6 +597,38 @@ Array
566597

567598
<hr />
568599

600+
### getRandomItems
601+
602+
**Description:**
603+
604+
Get random items from array.
605+
606+
If `$count` is greater than or equal to the number of items on the array,
607+
the original array is returned in random order.
608+
609+
**Parameters:**
610+
611+
- `$array` (array): Original array
612+
- `$count` (int): Number of random items to return
613+
614+
**Returns:**
615+
616+
- (array)
617+
618+
**Example:**
619+
620+
```
621+
$array = [
622+
'John',
623+
'Jane',
624+
'Bob'
625+
];
626+
627+
$array = Arr::getRandomItems($array, 1);
628+
```
629+
630+
<hr />
631+
569632
### query
570633

571634
**Description:**

src/Arr.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,18 @@ public static function except(array $array, array|string $keys): array
227227
return array_diff_key($array, array_flip((array)$keys));
228228
}
229229

230+
/**
231+
* Returns the original array except given value(s).
232+
*
233+
* @param array $array (Original array)
234+
* @param array|string $values (Values to remove)
235+
* @return array
236+
*/
237+
public static function exceptValues(array $array, array|string $values): array
238+
{
239+
return array_values(array_diff($array, (array)$values));
240+
}
241+
230242
/**
231243
* Returns only desired key(s) from an array.
232244
*
@@ -375,6 +387,36 @@ public static function order(array $array, array $order): array
375387
return self::only(array_replace(array_flip($order), $array), array_keys($array));
376388
}
377389

390+
/**
391+
* Get random items from array.
392+
*
393+
* If $count is greater than or equal to the number of items on the array,
394+
* the original array is returned in random order.
395+
*
396+
* @param array $array (Original array)
397+
* @param int $count (Number of random items to return)
398+
* @return array
399+
*/
400+
public static function getRandomItems(array $array, int $count): array
401+
{
402+
403+
if (count($array) <= $count) {
404+
$shuffled = $array;
405+
shuffle($shuffled);
406+
return $shuffled;
407+
}
408+
409+
$keys = array_rand($array, $count);
410+
411+
// Ensure $keys is always an array
412+
if (!is_array($keys)) {
413+
$keys = [$keys];
414+
}
415+
416+
return array_map(fn($key) => $array[$key], $keys);
417+
418+
}
419+
378420
/**
379421
* Convert array into a query string.
380422
*

0 commit comments

Comments
 (0)