Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:61633 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 15472 invoked from network); 23 Jul 2012 01:15:42 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Jul 2012 01:15:42 -0000 Authentication-Results: pb1.pair.com header.from=ronabop@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ronabop@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.42 as permitted sender) X-PHP-List-Original-Sender: ronabop@gmail.com X-Host-Fingerprint: 74.125.82.42 mail-wg0-f42.google.com Received: from [74.125.82.42] ([74.125.82.42:61796] helo=mail-wg0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CA/43-20357-DB5AC005 for ; Sun, 22 Jul 2012 21:15:41 -0400 Received: by wgbfm10 with SMTP id fm10so1893091wgb.5 for ; Sun, 22 Jul 2012 18:15:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=eg43Sp7yxRtdj1ecQ5krEXAKnm45Ocqckh0cEbRTC9k=; b=NiRK9iUkeUMO+83F81TwxWDDeH+KP8oNxsHJcruAqS2Jkbt/kPxKrmWvhmkMpZYn/b r8ndC8/4S1LFHsy+pEPDOx0Z6TDfo2For+PSDGhAazoE9kNMiQn4HsfGeG0HERLvwzMm /f2MapVFWxyduTpzYLO/PkgHZZBowsm9LxYbl9Z2MWfyO8p8kmWGpmQFqfhRMeXS2QnB /4j8XLb7lkf0nnJbmI7xipDLx6/jh61uxox+w2gV2vnNbaIWGIxTwkN0ZVu/OCFavNFW msjtRgql4jY3/8NS9CPGiuxReVi/yHHoZXL4496spL6bGPSSPa2MsHoTfPzVMUN2cAw5 7oYA== MIME-Version: 1.0 Received: by 10.180.84.104 with SMTP id x8mr43983805wiy.20.1343006138359; Sun, 22 Jul 2012 18:15:38 -0700 (PDT) Received: by 10.216.142.40 with HTTP; Sun, 22 Jul 2012 18:15:38 -0700 (PDT) In-Reply-To: <463291667.91403.1343002510557.JavaMail.open-xchange@oxwebmail.registrar-servers.com> References: <463291667.91403.1343002510557.JavaMail.open-xchange@oxwebmail.registrar-servers.com> Date: Sun, 22 Jul 2012 18:15:38 -0700 Message-ID: To: Andrew Faulds Cc: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] [proposal + patch] array_pick() function From: ronabop@gmail.com (Ronald Chmara) 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? On Sun, Jul 22, 2012 at 5:15 PM, Andrew Faulds wrote: > 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/ > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php >