Reputation: 159
I have a multidimensional array composed originally from post variables that looks something like this:
$easys = array(
array($easy1min,$easy1max,$easy1enc),
array($easy2min,$easy2max,$easy2enc),
array($easy3min,$easy3max,$easy3enc),
array($easy4min,$easy4max,$easy4enc),
array($easy5min,$easy5max,$easy5enc),
array($easy6min,$easy6max,$easy6enc),
array($easy7min,$easy7max,$easy7enc),
array($easy8min,$easy8max,$easy8enc),
array($easy9min,$easy9max,$easy9enc),
array($easy10min,$easy10max,$easy10enc)
);
I'm attempting to return one randomized result from this.
My function trying shuffle looks like this:
$shuffle($easy_encounters);
$num = rand($easy_encounters[0][0],$easy_encounters[0][1]);
return "(".$num.") ".$easy_encounters[0][2];
gives
"shuffle expect parameter 1 to be array.."
I have also tried iterator_to_array:
$easy_encounters = iterator_to_array($easy_encounters);
which returns error
"Catchable fatal error: Argument 1 passed to iterator_to_array() must implement interface Traversable..."
then a couple attempts using various syntaxes for array_rand such as:
$easy_encounters = array_rand($easy_encounters);
$num = rand($easy_encounters [0][0],$easy_encounters [0][1]);
return "(".$num.") ".$easy_encounters [0][2];
and
$random_obj = $easy_encounters[array_rand($easy_encounters)];
$num = rand($random_obj[0][0],$random_obj[0][1]);
return "(".$num.") ".$random_obj[0][2];
I feel like I'm hitting all around this. I admit perhaps not fully understanding the useage of iterator_to_array after I got that Traversable error.
Any help is appreciated. I've trudged around SO which is where I've gotten the examples i've used thusfar.
Upvotes: 0
Views: 2114
Reputation: 307
$randomArray = array_rand($easy_encounters);
echo $easy_encounters[$randomArray][array_rand($easy_encounters[$randomArray])];
First get a random array. Then get a random value from the array.
$randomArray
is a random array inside of $easy_encounters
. So the bottom line reads echo $easy_encounters[$randomArray][$randomElement inside $randomArray]
.
Upvotes: 2
Reputation: 362
First I don't think you can use $shuffle(argument)
is it a function not a variable, remove the $
, second you used $easys
in the first part of code and then $easy_encounters
to shuffle it. Use the same variable name in both of them.
Upvotes: 1