|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2009-04-18 03:17 UTC] rasmus@php.net
[2011-04-08 20:32 UTC] jani@php.net
-Package: Feature/Change Request
+Package: Arrays related
[2020-03-01 23:13 UTC] cmb@php.net
-Status: Open
+Status: Suspended
[2020-03-01 23:13 UTC] cmb@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 14:00:01 2025 UTC |
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.