How to get random element from multidimensional array in php -
i have multidimensional array composed post variables looks 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 return 1 randomized result this.
my function trying shuffle looks 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 array.."
i have tried iterator_to_array:
$easy_encounters = iterator_to_array($easy_encounters);
which returns error
"catchable fatal error: argument 1 passed iterator_to_array() must implement interface traversable..."
then couple attempts using various syntaxes 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 i'm hitting around this. admit perhaps not understanding useage of iterator_to_array after got traversable error.
any appreciated. i've trudged around i've gotten examples i've used thusfar.
$randomarray = array_rand($easy_encounters); echo $easy_encounters[$randomarray][array_rand($easy_encounters[$randomarray])];
first random array. random value array.
$randomarray
random array inside of $easy_encounters
. bottom line reads echo $easy_encounters[$randomarray][$randomelement inside $randomarray]
.
Comments
Post a Comment