0

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.

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Nallware
  • 159
  • 1
  • 4
  • 18
  • 2
    You called your array $easy but you use it everywhere as $easy_encounters – Orangepill Aug 07 '15 at 17:53
  • possible duplicate of [PHP Parse/Syntax Errors; and How to solve them?](http://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – Jay Blanchard Aug 07 '15 at 18:00
  • @Orangepill Sorry, I forgot to show that I assigned the array to a session, then session back again... `code` $_SESSION["enc_easy"] = $easys; $easy_encounters = $_SESSION["enc_easy"]; – Nallware Aug 07 '15 at 18:20
  • @JayBlanchard This is a specific question about randomizing a multi-dimensional array. I don't understand how you believe this is a duplicate of a very general question regarding Parse/Syntax errors. – Nallware Aug 07 '15 at 18:33
  • The issue is stated pretty clearly in the error message.... you aren't getting the proper function an array... – Orangepill Aug 07 '15 at 18:47
  • Are you looking to pick randomly from $easys and then take a random value from the inner array? $index = rand ( 0 , count($easys) ); $inner_index = rand(0, count($easys[$index])); echo($easys[$index][$inner_index]); – Jesse Adam Aug 07 '15 at 19:05
  • Its for a game. Each child of the array contains a minimum number encountered, a maximum number encountered, and the opponent. Ex: 2,5,Orc (all from $_POST's). I save these in a parent array then into a $_SESSION variable so I can keep them for "Roll Again". So each random child element should produce (using rand()) Ex: 4 Orc. – Nallware Aug 07 '15 at 20:14

2 Answers2

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.

b14r
  • 332
  • 5
  • 18
  • These are typos from going back and forth between various iterations of code I used when solving the problem – Nallware Aug 07 '15 at 18:22
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].

mattslone
  • 297
  • 1
  • 2
  • 14
  • Gives Warning: array_rand() expects parameter 1 to be array, null given – Nallware Aug 07 '15 at 18:27
  • It's working for me. Be sure to change the variable names. – mattslone Aug 07 '15 at 18:33
  • To make certain I understand, I take my array (in this case it's now called $easy_encounters and randomize to $randomArray, which is then put back into $easy_encounters? That's what it looks to me like your code is doing. – Nallware Aug 07 '15 at 18:39
  • This is what's happening. Let's say that it randomly chooses the first element in the first array inside `$easy_encounters`. `$randomArray` is the first array inside of `$easy_encounters`, in this case `$easy_encounters[0]`. So the bottom line reads `echo $easy_encounters[0][0]`. That second zero comes from `array_rand($easy_encounters[$randomArray])`, assuming it randomly chooses the first element inside that array. I know I didn't explain that very well, so let me know if you have questions. – mattslone Aug 07 '15 at 18:50
  • I've decided to do a work-around, but accepted your answer as the best. At least it was actually constructive. – Nallware Aug 07 '15 at 19:02