#48010 [NEW]: Returning element value from array_rand()

From: Date: Sat, 18 Apr 2009 03:09:02 +0000
Subject: #48010 [NEW]: Returning element value from array_rand()
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-135943@lists.php.net to get a copy of this message
From: me at 46bit dot com Operating system: All PHP version: 5.3.0RC1 PHP Bug Type: Feature/Change Request Bug description: Returning element value from array_rand() Description: ------------ With array_rand() as it is now, it only returns the key of the element(s) it picks randomly from the array. The problem with this is that much/most of the time all the programmer wants is to get a random value from that array - not the key. At present you then have to do code such as $arrayName[array_rand($arrayName)] or a foreach loop when getting multiple random elements. Whilst of limited importance, this could quite be implemented via the addition of a boolean parameter on the end of array_rand without too much trouble. Reproduce code: --------------- <?php $array = array('a', 'b', 'c'); $values = array_rand($array, 2, TRUE); // return the value if TRUE, the key if FALSE (FALSE the default in order to mirror current functionality) ?> Would be desired to get 2 random values from $array, as opposed having to do: <?php $array = array('a', 'b', 'c'); $values = array_rand($array, 2); foreach($values as &$value) { $value = $array[$value]; } ?> Expected result: ---------------- 2 Random values from $array. -- Edit bug report at http://bugs.php.net/?id=48010&edit=1 -- Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=48010&r=trysnapshot52 Try a CVS snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=48010&r=trysnapshot53 Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=48010&r=trysnapshot60 Fixed in CVS: http://bugs.php.net/fix.php?id=48010&r=fixedcvs Fixed in CVS and need be documented: http://bugs.php.net/fix.php?id=48010&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=48010&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=48010&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=48010&r=needscript Try newer version: http://bugs.php.net/fix.php?id=48010&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=48010&r=support Expected behavior: http://bugs.php.net/fix.php?id=48010&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=48010&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=48010&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=48010&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=48010&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=48010&r=dst IIS Stability: http://bugs.php.net/fix.php?id=48010&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=48010&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=48010&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=48010&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=48010&r=mysqlcfg

« previous php.bugs (#135943) next »