Skip to content

Commit

Permalink
document arr keyby
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Feb 22, 2022
1 parent f6286d4 commit 8a66b35
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions helpers.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Laravel includes a variety of global "helper" PHP functions. Many of these funct
[Arr::hasAny](#method-array-hasany)
[Arr::isAssoc](#method-array-isassoc)
[Arr::isList](#method-array-islist)
[Arr::keyBy](#method-array-keyby)
[Arr::last](#method-array-last)
[Arr::only](#method-array-only)
[Arr::pluck](#method-array-pluck)
Expand Down Expand Up @@ -599,6 +600,27 @@ The `Arr::isList` method returns `true` if the given array's keys are sequential

// false

<a name="method-array-keyby"></a>
#### `Arr::keyBy()` {.collection-method}

The `Arr::keyBy` method keys the array by the given key. If multiple items have the same key, only the last one will appear in the new array:

use Illuminate\Support\Arr;

$array = [
['product_id' => 'prod-100', 'name' => 'Desk'],
['product_id' => 'prod-200', 'name' => 'Chair'],
];

$keyed = Arr::keyBy($array, 'product_id');

/*
[
'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'],
'prod-200' => ['product_id' => 'prod-200', 'name' => 'Chair'],
]
*/

<a name="method-array-last"></a>
#### `Arr::last()` {.collection-method}

Expand Down

0 comments on commit 8a66b35

Please sign in to comment.