# [proposal + patch] array_pick() function

- Thread: https://externals.io/message/61632
- Original archive: https://news-web.php.net/php.internals/61632
- Started by Andrew Faulds on 2012-07-23T00:15:10+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #61632 — Andrew Faulds — 2012-07-23T00:15:10+00:00

URL: https://externals.io/message/61632#61632

Hi there,

This is a small function, called array_pick(), which takes one argument
($array), an array, and returns the value of a random item.

Much of its usefulness stems from the new short array syntax, which lets you do
array_pick(["funlogo.png", "coollogo.jpg", "kittens.gif"]);, instead of $a =
array("funlogo.png", "coollogo.jpg", "kittens.gif"); $a = $a[array_rand($a)];
It's also useful for picking a random value out of a none-literal array. For
instance today it would have been handy for my markov chain propaganda generator
(not the most practical example, I realise.)

However, of course, PHP already has `array_rand()`, and an equivalent function
could be implemented in 3 lines of code. The reason I'd like it added to PHP
though is largely convenience. It's in at least two other languages I use
(Python's random.choice(), Game Maker/GML's choose()) and I use it quite often
in different projects (just today I've needed it 4 times in PHP code). It's also
the logical counterpart to `array_rand()`, which gets a random key, not a random
value, so it would help API completeness. I'm pretty sure I'm not the only
person who wants to fetch a random array value, not a random array key, too.
It's useful also in the scenario where you want a random value of nested arrays
($array['thing']['foo']).

Enough rambling, here's a pull request. https://github.com/php/php-src/pull/142
<https://github.com/php/php-src/pull/142>

--
Andrew Faulds
http://ajf.me/

---

## #61633 — Ronald Chmara — 2012-07-23T01:15:38+00:00

URL: https://externals.io/message/61632#61633 · In reply to: #61632

Standard hazing:
1. Tests?
2. Docs?

Annoying if needed whining:
I think "array_pick" and "array_rand" are precisely the kind of
function naming that makes PHP infamous. I would suggest that
array_rand_key() and array_rand_value(), or `array_rand()` with an
argument to default to key (for BC).

Speed testing: Are there any metrics to support/prove this is faster
than existing PHP calls to do the same thing?


>  Hi there,
>
> This is a small function, called array_pick(), which takes one argument
> ($array), an array, and returns the value of a random item.
>
> Much of its usefulness stems from the new short array syntax, which lets you do
> array_pick(["funlogo.png", "coollogo.jpg", "kittens.gif"]);, instead of $a =
> array("funlogo.png", "coollogo.jpg", "kittens.gif"); $a = $a[array_rand($a)];
> It's also useful for picking a random value out of a none-literal array. For
> instance today it would have been handy for my markov chain propaganda generator
> (not the most practical example, I realise.)
>
> However, of course, PHP already has `array_rand()`, and an equivalent function
> could be implemented in 3 lines of code. The reason I'd like it added to PHP
> though is largely convenience. It's in at least two other languages I use
> (Python's random.choice(), Game Maker/GML's choose()) and I use it quite often
> in different projects (just today I've needed it 4 times in PHP code). It's also
> the logical counterpart to `array_rand()`, which gets a random key, not a random
> value, so it would help API completeness. I'm pretty sure I'm not the only
> person who wants to fetch a random array value, not a random array key, too.
> It's useful also in the scenario where you want a random value of nested arrays
> ($array['thing']['foo']).
>
> Enough rambling, here's a pull request. https://github.com/php/php-src/pull/142
> <https://github.com/php/php-src/pull/142>
>
> --
> Andrew Faulds
> http://ajf.me/
