Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:61632 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 12503 invoked from network); 23 Jul 2012 00:15:13 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Jul 2012 00:15:13 -0000 Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ajf.me designates 64.22.89.133 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 64.22.89.133 oxmail.registrar-servers.com Linux 2.6 Received: from [64.22.89.133] ([64.22.89.133:56813] helo=oxmail.registrar-servers.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 7F/D2-20357-0979C005 for ; Sun, 22 Jul 2012 20:15:13 -0400 Received: from app2.ox.registrar-servers.com (app2.ox.registrar-servers.com [172.20.180.4]) (using TLSv1 with cipher RC4-MD5 (128/128 bits)) (No client certificate requested) by oxmail.registrar-servers.com (Postfix) with ESMTPSA id 97DF3C30006 for ; Sun, 22 Jul 2012 20:15:10 -0400 (EDT) Date: Mon, 23 Jul 2012 01:15:10 +0100 (BST) Reply-To: Andrew Faulds To: internals@lists.php.net Message-ID: <463291667.91403.1343002510557.JavaMail.open-xchange@oxwebmail.registrar-servers.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Priority: 3 Importance: Medium X-Mailer: Open-Xchange Mailer v6.20.3-Rev2 Subject: [proposal + patch] array_pick() function From: ajf@ajf.me (Andrew Faulds) 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 -- Andrew Faulds http://ajf.me/