Arr::unwrap
and single
methods
#42205
Unanswered
michael-rubel
asked this question in
Ideas
Replies: 1 comment
-
I need this function too. I think it migth be more elegant and helpful. Function code: public static function unwrap($value)
{
if (is_countable($value) && count($value) === 1) {
return self::first($value);
}
return $value;
} Usage: /**
* @param string|string[]|Collection $input single or many
* @return string|string[] or smth else
*/
public function foo(string|array|Collection $input): string|array
{
$output = [];
foreach (Arr::wrap($input) as $item) {
// Do smth
$output[$item] = 'bar';
}
// if (count($output) === 1) {
// return Arr::first($output);
// }
//
// return $output;
return Arr::unwrap($output);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, Laravel Folks!
I always miss these features when building packages around Laravel:
count === 1
but with some beautiful syntax method, for example,single
, orone
.Arr::unwrap
method, to return value from an array if it contains only one key inside.I thought about PRs, but still not sure it is worth an effort and wouldn't be rejected. What do you think?
Beta Was this translation helpful? Give feedback.
All reactions