-
-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pluckMany dotted key support #248
Conversation
Dear contributor, because this pull request seems to be inactive for quite some time now, I've automatically closed it. If you feel this pull request deserves some attention from my human colleagues feel free to reopen it. |
@spatie-bot Dear unhelpful bot, I cannot control the reactions of your masters. |
Could you document this? |
Dear human :) How would you like it documented, it's how lodash (though not underscore) behaves when a string key is specified to It's also how Collection::pluck operates. If you would permit me a slight rudeness, you should be documenting why your pluckMany doesn't operated on dotted values. public static function pluck($array, $value, $key = null)
{
$results = [];
[$value, $key] = static::explodePluckParameters($value, $key);
foreach ($array as $item) {
$itemValue = data_get($item, $value); function data_get($target, $key, $default = null)
{
if (is_null($key)) {
return $target;
}
$key = is_array($key) ? $key : explode('.', $key);
foreach ($key as $i => $segment) {
unset($key[$i]);
if (is_null($segment)) {
return $target;
}
if ($segment === '*') {
if ($target instanceof Collection) {
$target = $target->all();
} elseif (! is_iterable($target)) {
return value($default);
}
$result = [];
foreach ($target as $item) {
$result[] = data_get($item, $key);
}
return in_array('*', $key) ? Arr::collapse($result) : $result;
}
// ... |
Great macro. When will be merged 👀 |
Thanks! |
To bring
pluckMany
inline with various otherpluck
implementations, includingCollections::pluck
(Arr::pluck
), this allows the specification of dotted keys.e.g.,