note 97625 added to function.array-rand
| From: | dtbakergmail at osu1 dot php dot net | Date: | Thu, 29 Apr 2010 04:08:51 +0000 |
| Subject: | note 97625 added to function.array-rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168771@lists.php.net to get a copy of this message | ||
For those who find that array_rand returns nothing, make sure the $num_req parameter isn't more
than the number of elements in the array.
<?php
$foo = array(1,2);
$result = array_rand($foo,6);
// $result is nothing PHP5.2.4
?>
try this instead, if you don't have control over the number of elements in the input array.
<?php
$foo = array(1,2);
$result = array_rand($foo,min(count($foo),6));
// $result contains 2 items.
?>
----
Server IP: 117.55.229.1
Probable Submitter: 121.208.5.105
----
Manual Page -- http://www.php.net/manual/en/function.array-rand.php
Edit -- https://master.php.net/note/edit/97625
Del: integrated -- https://master.php.net/note/delete/97625/integrated
Del: useless -- https://master.php.net/note/delete/97625/useless
Del: bad code -- https://master.php.net/note/delete/97625/bad+code
Del: spam -- https://master.php.net/note/delete/97625/spam
Del: non-english -- https://master.php.net/note/delete/97625/non-english
Del: in docs -- https://master.php.net/note/delete/97625/in+docs
Del: other reasons-- https://master.php.net/note/delete/97625
Reject -- https://master.php.net/note/reject/97625
Search -- https://master.php.net/manage/user-notes.php