ID:               48010
 Updated by:       [email protected]
 Reported By:      me at 46bit dot com
 Status:           Open
 Bug Type:         Feature/Change Request
 Operating System: All
 PHP Version:      5.3.0RC1
 New Comment:

A very common thing to do is to unset the random element from the array
so you never pick the same one twice.  In order to do that you have to
return the index of the element.  Returning the value is useless.  And
since it is trivial to go from index to value, that seemed like the
logical way to implement this.  


Previous Comments:
------------------------------------------------------------------------

[2009-04-18 03:09:02] me at 46bit dot com

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 this bug report at http://bugs.php.net/?id=48010&edit=1

Reply via email to