I have a multidimensional array composed originally from post variables that looks something like this:
我有一个多维数组,最初由post变量组成,看起来像这样:
$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看起来像这样:
$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.."
“shuffle期望参数1为数组..”
I have also tried iterator_to_array:
我也试过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..."
“可捕获的致命错误:传递给iterator_to_array()的参数1必须实现Traversable接口......”
then a couple attempts using various syntaxes for array_rand such as:
然后有几次尝试使用array_rand的各种语法,例如:
$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.
我觉得我正在打这个。我承认在得到Traversable错误后可能还没有完全理解iterator_to_array的用法。
Any help is appreciated. I've trudged around SO which is where I've gotten the examples i've used thusfar.
任何帮助表示赞赏。我已经在SO周围跋涉,这就是我已经得到了我用过的例子。
2 个解决方案
#1
1
$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]
.
$ randomArray是$ easy_encounters中的随机数组。所以底线读取$ $ easy_encounters [$ randomArray] [$ randomElement里面$ randomArray]。
#2
1
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.
首先我不认为你可以使用$ shuffle(参数)它是一个函数而不是一个变量,删除$,第二个你在代码的第一部分使用$ easys然后$ easy_encounters来改变它。在它们中使用相同的变量名称。
#1
1
$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]
.
$ randomArray是$ easy_encounters中的随机数组。所以底线读取$ $ easy_encounters [$ randomArray] [$ randomElement里面$ randomArray]。
#2
1
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.
首先我不认为你可以使用$ shuffle(参数)它是一个函数而不是一个变量,删除$,第二个你在代码的第一部分使用$ easys然后$ easy_encounters来改变它。在它们中使用相同的变量名称。