I have an array with 30 element like this:
$array = ["1","2",....,"29","30"];
On a table of 10 rows, I need to distribute these elements in rows, in which each row takes three random elements from the array, without duplication of the elements in each row.
How can I accomplish this in PHP?
Thanks for your answers, it was very helpful. After many trials, i wrote the below code:
<?
$array1 = ['one', 'two', 'three', 'four', 'five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen'];
function RandomArrayNew($array){
$keyrandom = array_rand($array, 3);
$a= $array[$keyrandom[0]];
$b= $array[$keyrandom[1]];
$c= $array[$keyrandom[2]];
$t = [$a, $b, $c];
return $t;
}
?>
<table style="width: 100%" class="bodymenu">
<?
$countRows = 5;
for ($i = 0; $i <= $countRows; $i++){
$x = RandomArrayNew($array1);
$y =array_diff($array1, [$x[0], $x[1], $x[2]]);
echo "<tr>";
echo"<td><input type='text' value='".$x[0]."'/></td>";
echo"<td><input type='text' value='".$x[1]."'/></td>";
echo"<td><input type='text' value='".$x[2]."'/></td>";
echo "</tr>";
}
?>
</table>
This code actually draws 5 rows by for loop and in each row a randomly distribute three numbers. the problem is the duplication of distributed numbers. I need to prevent duplicated numbers between rows. if it possible to unset the numbers distributed form the original array and use the new array in next row? and if yes how can i do it?