A::keyBy()
Takes an array and makes it associative by an argument. If it is a string, it will be used as a key to pluck from the array. Otherwise, the callable will be used to map the array.
A::keyBy(array $array, string|callable(mixed): array-key $keyBy): array
Parameters
| Name | Type | Default |
|---|---|---|
| $arrayrequired | array<mixed> |
no default value |
| $keyByrequired | stringorcallable(mixed): array-key |
no default value |
Return type
array<array-key, mixed>
Parent class
Examples
$array = [['id' => 1], ['id' => 2], ['id' => 3]];
$keyed = A::keyBy($array, 'id');
// result: [1 => ['id' => 1], 2 => ['id' => 2], 3 => ['id' => 3]]