note 120048 added to function.array-rand
| From: | divinity76 at gmail dot com | Date: | Sat, 15 Oct 2016 13:59:21 +0000 |
| Subject: | note 120048 added to function.array-rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-206900@lists.php.net to get a copy of this message | ||
if you're looking for a cryptographically secure variant (as of speaking, php uses mt_rand), i
made this. (limitations: it has no $num parameter. it requires php>=7)
<?php
function csarray_rand(array $arr){
$keys=array_keys($arr);
$count=count($keys);
if($count===0){
//Contrary to documentation, PHP 4 and 5 and 7, up to php 7.1.0alpha3 does not actually
return any warning when the array is empty and $num=NULL (and further contradicting the
documentation, its NULL by default, not 1, and they are treated differently, 1 gives error, NULL
does not.) ... 7.1.0beta1 does throw an error however... follow their example?
if(version_compare(PHP_VERSION,'7.1.0beta1','>=')){
trigger_error('Warning: csarray_rand(): Array is empty',E_USER_ERROR);
}
return NULL;
}
$csrand=random_int(0,count($keys)-1);
return $keys[$csrand];
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 88.88.19.218
----
Manual Page -- http://php.net/manual/en/function.array-rand.php
Edit -- https://master.php.net/note/edit/120048
Del: integrated -- https://master.php.net/note/delete/120048/integrated
Del: useless -- https://master.php.net/note/delete/120048/useless
Del: bad code -- https://master.php.net/note/delete/120048/bad+code
Del: spam -- https://master.php.net/note/delete/120048/spam
Del: non-english -- https://master.php.net/note/delete/120048/non-english
Del: in docs -- https://master.php.net/note/delete/120048/in+docs
Del: other reasons-- https://master.php.net/note/delete/120048
Reject -- https://master.php.net/note/reject/120048
Search -- https://master.php.net/manage/user-notes.php