Skip to content

Commit

Permalink
group()->pluck('key')
Browse files Browse the repository at this point in the history
  • Loading branch information
donwilson committed Dec 1, 2023
1 parent 09d3b07 commit 754ae42
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Magnetar/Group/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,23 @@ public function values(): static {
return new static(array_values($this->items));
}

/**
* Pluck values from the items in the group by a shared key
* @param string $key
* @return static
*/
public function pluck(string $key): static {
$values = [];

foreach($this->items as $item) {
if(isset($item[ $key ])) {
$values[] = $item[ $key ];
}
}

return new static($values);
}

/**
* Make a new group with the keys and values flipped
* @return static
Expand Down

0 comments on commit 754ae42

Please sign in to comment.