Skip to content

[5.4] Add $num optional parameter to Arr::random() - #19818

Merged
taylorotwell merged 2 commits into
laravel:5.4from
vlakoff:array_random
Jun 29, 2017
Merged

[5.4] Add $num optional parameter to Arr::random()#19818
taylorotwell merged 2 commits into
laravel:5.4from
vlakoff:array_random

Conversation

@vlakoff

@vlakoff vlakoff commented Jun 29, 2017

Copy link
Copy Markdown
Contributor

Follow-up to #19741.

There is a bit of room for improvement. Mainly, the function would fail if requesting more items than available. This can be dealt with later, here I do this PR to quickly alter the method signature while it is fresh.

ping @calebporzio

@taylorotwell
taylorotwell merged commit 53f6d52 into laravel:5.4 Jun 29, 2017
@vlakoff

vlakoff commented Jun 29, 2017

Copy link
Copy Markdown
Contributor Author

array_random($array, 1) behaves as array_random($array), i.e. it directly returns the random item.

Is it ok, or should it be changed to behave as array_random($array, N), i.e. returning an array of one item?

@vlakoff

vlakoff commented Jun 29, 2017

Copy link
Copy Markdown
Contributor Author

Indeed that's what Collection::random() does. random() directly returns the item, random(1) returns an array of one item. Do you agree Arr::random() should do the same?

@calebporzio

Copy link
Copy Markdown
Contributor

I think so, it more closely matches array_rand as well - I will submit a PR as soon as I get the chance.

@vlakoff

vlakoff commented Jun 29, 2017

Copy link
Copy Markdown
Contributor Author

I'm already on it :) Also, refs #16865.

@calebporzio

Copy link
Copy Markdown
Contributor

Cool 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants