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!

Recommended Answers

All 10 Replies

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);

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.

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

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

Member Avatar for rajarajan2017

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

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!

Member Avatar for rajarajan2017

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

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.