Page 1 of 1

Array Rand

Posted: Tue Nov 04, 2008 10:38 am
by $var
http://ca.php.net/array_rand

When using array_rand, I am getting no values at all.

The array ($story) returns an array of 7 elements, when not passed through array_rand
I want to get 4 of the 7 elements randomly.

$story = function($getThese, 7)
$story = array_rand($story ,4);

When using array_rand, my var_dump shows this:
array(4) { [0]=> int(1) [1]=> int(4) [2]=> int(2) [3]=> int(3) }

I am using a multi-dimensional array.

Re: Array Rand

Posted: Tue Nov 04, 2008 12:09 pm
by Mark Baker
$var wrote:http://ca.php.net/array_rand

When using array_rand, I am getting no values at all.

$story = function($getThese, 7)
$story = array_rand($story ,4);
Yes you are, you're getting a set of keys exactly as the manual says you will.
But then you're overwriting the original array with this new array of keys.

Code: Select all

 
$storyKeys = array_rand($story ,4);
for each ($storyKeys as $storyKey) {
    print_r($story[$storyKey]);
}