Skip to content

Commit

Permalink
[10.x] Arr::select not working when $keys is a string (#50169)
Browse files Browse the repository at this point in the history
* Arr::select not working when $keys is a string

Adding a check about $keys type.

If it is a string, transform it into an array.

If not, it throws a foreach() error

* add test

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
Sicklou and taylorotwell authored Feb 21, 2024
1 parent abeec17 commit e708043
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Illuminate/Collections/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,8 @@ public static function only($array, $keys)
*/
public static function select($array, $keys)
{
$keys = static::wrap($keys);

return static::map($array, function ($item) use ($keys) {
$result = [];

Expand Down
36 changes: 36 additions & 0 deletions tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1273,4 +1273,40 @@ public function testTake()
4, 5, 6,
], Arr::take($array, -3));
}

public function testSelect()
{
$array = [
[
'name' => 'Taylor',
'role' => 'Developer',
'age' => 1,
],
[
'name' => 'Abigail',
'role' => 'Infrastructure',
'age' => 2,
],
];

$this->assertEquals([
[
'name' => 'Taylor',
'age' => 1,
],
[
'name' => 'Abigail',
'age' => 2,
],
], Arr::select($array, ['name', 'age']));

$this->assertEquals([
[
'name' => 'Taylor',
],
[
'name' => 'Abigail',
],
], Arr::select($array, 'name'));
}
}

0 comments on commit e708043

Please sign in to comment.