0

I do have the way to get a randomized elements in array:

$array1 = array("Bird", "Animal", "Tower", "Car", "Tree");
$rand_keys = array_rand($input, 4);

echo $array1[$rand_keys[0]] . "\n";
echo $array1[$rand_keys[1]] . "\n";
echo $array1[$rand_keys[2]] . "\n";
echo $array1[$rand_keys[3]] . "\n";

but, if I had 3 arrays those I can randomize their elements, too:

$array1 = array("Bird", "Animal", "Tower", "Car", "Tree");
$array2 = array("Falcon", "Cat", "Man", "Poll", "Sea");
$array3 = array("Lion", "Apple", "Rock", "Ball", "Tea");

How could I get a the vertical randomization of the arrays themselves?

I need the result to be like these patterns:

Car Tree Bird Animal Tower  // array1
Man Poll Falcon Cat Sea     // array2
Rock Ball Tea Lion Apple    // array3

Another one if I refreshed the page:

Lion Apple Rock Ball Tea    // array3
Animal Tower Car Tree Bird  // array1
Man Cat Sea Poll Falcon     // array2
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
MRAN
  • 127
  • 2
  • 12

4 Answers4

0

You can use array_merge

$arrays = array_merge($array1, $array2, $array3);
$rand_keys = array_rand($arrays, 4);

Edit:

It appears you want to shuffle:

$arrays[0] = array("Bird", "Animal", "Tower", "Car", "Tree");
$arrays[1] = array("Falcon", "Cat", "Man", "Poll", "Sea");
$arrays[2] = array("Lion", "Apple", "Rock", "Ball", "Tea");

foreach (array_keys($arrays) as $key) {
    shuffle($arrays[$key]);
};
shuffle($arrays);

foreach ($arrays as $array) {
    print_r($array);
}
webbiedave
  • 48,414
  • 8
  • 88
  • 101
  • I don't want to merge the arrays, I need each array to be separate. Please check the update. – MRAN May 24 '13 at 14:23
0
 $array1= array("Bird", "Animal", "Tower", "Car", "Tree");
 $array2= array("Falcon", "Cat", "Man", "Poll", "Sea");
 $array3= array("Lion", "Apple", "Rock", "Ball", "Tea");
 $arr = array_merge($array1,$array2,$array3);
 shuffle($arr);
 print_r($arr);

Merge these arrays with array_merge() and use shuffle()

Robert
  • 19,800
  • 5
  • 55
  • 85
0
$array = array(
  array('Bird', 'Animal', 'Tower', 'Car', 'Tree'),
  array('Falcon', 'Cat', 'Man', 'Poll', 'Sea'),
  array('Lion', 'Apple', 'Rock', 'Ball', 'Tea')
);

array_walk($array, function (&$array) { shuffle($array); });
shuffle($array);

var_dump($array);
CBroe
  • 91,630
  • 14
  • 92
  • 150
  • I have extra question please: I have 5 elements in each array, how can I drop the element of position 0 from the result? I mean I want to get the elements of positions from 1 to 4 only? More clarification, I don't need 'Bird', 'Falcon' and 'Lion' to get involve in the shuffling every time. – MRAN May 24 '13 at 16:17
  • Well, that’s easy as well – just throw the first element away before shuffling the under-arrays, using array_shift: `array_walk($array, function (&$array) { array_shift($array); shuffle($array); });` – CBroe May 25 '13 at 15:10
0

Shuffle each row, then shuffle the main array.

foreach($mainArray as &row)
  shuffle($row);

shuffle($mainArray);
moonwave99
  • 21,957
  • 3
  • 43
  • 64