0

Hi,

*I need to randomize a multidimensional array so that the order of questions in a questionnaire is different for each participant.

I have limited php skills, and am modifying an old script someone else wrote. Currently, questions are presented in the same order that they are listed in the array. I need them to be presented in a different random order each time. The array is an array made up of other arrays and looks like this:

$original_items = array(

array(1,'stuff','info',response1,'info',response2'),		

array(2,'stuff','info',response1,'info',response2'),		
array(3,'stuff','info',response1,'info',response2'),		
array(4,'stuff','info',response1,'info',response2'),

*I've tried shuffle. It presents random items but I need each item in the array to be presented exactly once. I also have a short randomizing script we used to use, but it makes 1 random order, not a new one each time, e.g. (for an array with 100 items)

//for($count=0; $count<100; $count++){

	for($index=100; $index>=0; $index--){

		if(count % 2 == 0 && index % 2 == 0){

			$temp = $items[$count];

			$items[$count] = $items[$index];

			$items[$index] = $temp;

		}

	}

}

Any help appreciated!

6
Contributors
10
Replies
11
Views
7 Years
Discussion Span
Last Post by cgi.net
0

So you do not need to shuffle multidimensional array (this implies that elements of 'subarrays' also need to be shuffled)

you jsut need to shuffle elements of the 'parent' array.

This can be done by standard PHP function shuffle()

$original_items = array(
       array(1,'stuff','info',response1,'info',response2'),
     array(2,'stuff','info',response1,'info',response2'),
      array(3,'stuff','info',response1,'info',response2'),
       array(4,'stuff','info',response1,'info',response2'),
);
shuffle($original_items);
0

Unfortunaley, shuffle doesn't work because it merely takes random values, allowing repeats and also skipping some items. E.g., for my example array with 4 items, shuffle will present four items, but that can be "item 4, item 2, item 3, item 2".

I need to present each item once, without skipping any items.

But thanks for the tip:I guess I don't have a multidimensional array.

0

Seems to me that you have an array with 100 elments and want to take 4 random but unique ones, right?

0

Why not put the questions in a database table and randomly select the row id?

0

Logic:

<?php
$filter_field = array();
$original_items = array(
array(1, 'stuff1', 'info1', 'response1', 'info1', 'response1'), array(2, 'stuff2', 'info2', 'response2', 'info2', 'response2'), array(3, 'stuff3', 'info3', 'response3', 'info3', 'response3'), array(4, 'stuff4', 'info4', 'response4', 'info4', 'response4'));

for ($x = 0; $x < sizeof($original_items); $x++) { 
   array_push($filter_field, $original_items[$x][0]);
} 

shuffle($filter_field);

echo "<br/><br/><br/>";

for ($x = 0; $x < sizeof($original_items); $x++) { 
$k = $filter_field[$x];
 for ($y = 0; $y < 5; $y++) {
	echo $original_items[$k-1][$y];
 }
 echo "<br/><br/>";
}
?>

Execute the code it will return random array values

0

Seems to me that you have an array with 100 elments and want to take 4 random but unique ones, right?

Hi!

No, I actually have an array with 100 elements, and I want to take each one once in a random order, without skipping any element and without repeating any element. The example array with 4 elements was just to give an example.

Hope that clarifies things!

0

Hi JennyK, hope my code will work with anynumber of elements. Will you tested it?

Edited by rajarajan07: n/a

0

Pass it on to new arrays, do a rough check if the values for each index is equal if not reshuffle it.

0

JennyK

Unfortunately your conditions are not clear.
So I have to guess.

Look -- shuffle() is just a 'mixer' function, it will not repeat any of initial array elements (provising initial array had no repeats on its own), nor will shuffle() skip any elements.

If you had array with 100 elements shuffle() will return same array with 100 same elements but in random order inside the array.

Please clearly describe the task, like
"I have array with many elements (100+) in this format: 'here you put little piece of it' and I want to get array with (for example, -- I'm guessing here) 4 elements taken from initial array in random order with no element repeated"

//advice: consider to hire some professional

0

hi! try
--------------
$numbers = range(0, count($original_items));
shuffle($numbers);

for($i=0;$i< count($original_items);$i++):
$item[$i] = $original_items[$numbers[$i]];
endfor;

[]´s
Henrique F.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.