Skip to content

Commit e708043

Browse files
[10.x] Arr::select not working when $keys is a string (#50169)
* 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 <taylor@laravel.com>
1 parent abeec17 commit e708043

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/Illuminate/Collections/Arr.php

+2
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,8 @@ public static function only($array, $keys)
516516
*/
517517
public static function select($array, $keys)
518518
{
519+
$keys = static::wrap($keys);
520+
519521
return static::map($array, function ($item) use ($keys) {
520522
$result = [];
521523

tests/Support/SupportArrTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -1273,4 +1273,40 @@ public function testTake()
12731273
4, 5, 6,
12741274
], Arr::take($array, -3));
12751275
}
1276+
1277+
public function testSelect()
1278+
{
1279+
$array = [
1280+
[
1281+
'name' => 'Taylor',
1282+
'role' => 'Developer',
1283+
'age' => 1,
1284+
],
1285+
[
1286+
'name' => 'Abigail',
1287+
'role' => 'Infrastructure',
1288+
'age' => 2,
1289+
],
1290+
];
1291+
1292+
$this->assertEquals([
1293+
[
1294+
'name' => 'Taylor',
1295+
'age' => 1,
1296+
],
1297+
[
1298+
'name' => 'Abigail',
1299+
'age' => 2,
1300+
],
1301+
], Arr::select($array, ['name', 'age']));
1302+
1303+
$this->assertEquals([
1304+
[
1305+
'name' => 'Taylor',
1306+
],
1307+
[
1308+
'name' => 'Abigail',
1309+
],
1310+
], Arr::select($array, 'name'));
1311+
}
12761312
}

0 commit comments

Comments
 (0)