0

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?

0

3 Answers 3

1

Shuffle the array and split it into chunks of three:

$numbers = range(1, 30);
shuffle($numbers);
$chunks = array_chunk($numbers, 3);

Sample output of $chunks:

Array
(
    [0] => Array
        (
            [0] => 22
            [1] => 29
            [2] => 3
        )

    [1] => Array
        (
            [0] => 19
            [1] => 13
            [2] => 30
        )

    [2] => Array
        (
            [0] => 16
            [1] => 5
            [2] => 27
        )

    [3] => Array
        (
            [0] => 25
            [1] => 10
            [2] => 9
        )

    [4] => Array
        (
            [0] => 11
            [1] => 17
            [2] => 20
        )

    [5] => Array
        (
            [0] => 23
            [1] => 4
            [2] => 28
        )

    [6] => Array
        (
            [0] => 6
            [1] => 12
            [2] => 15
        )

    [7] => Array
        (
            [0] => 7
            [1] => 21
            [2] => 26
        )

    [8] => Array
        (
            [0] => 8
            [1] => 2
            [2] => 24
        )

    [9] => Array
        (
            [0] => 18
            [1] => 1
            [2] => 14
        )

)
Sign up to request clarification or add additional context in comments.

3 Comments

I get duplicates I I repeat the last 2 lines few times.
You need to add $numbers = array_slice($numbers, 3); so that the array_slice($numbers, 0, 3); code can be run multiple times to get different sets of random numbers.
Or even simpler: use array_chunk to split it up. I've updated my answer.
0

A general idea would be to get a random number and splice an array out. The array will get smaller and the next time you splice again you will not be able to pick the same element. However this way you are modifying the original array, so before you do it make a copy of the original if you need it later on.

function giveMeRandom(array &$arrayAsRef) {
    if (!$arrayAsRef) {
        return null;
    }
    // Splice a random element between 0 and size of array - 1, and return it
    return array_splice($arrayAsRef, mt_rand(0, count($arrayAsRef) - 1), 1)[0];
}

$myArr = range(1, 30);

var_dump(giveMeRandom($myArr));
var_dump(giveMeRandom($myArr));
var_dump(giveMeRandom($myArr));

Of course you could modify this function to return an array of 3 elements at a time.

Comments

0

welcome to Stackoverflow

To get help, you must provider some code example that you tried

Because you're new here there is a snippet here that should do the job

$array = range(1, 30);
$countRows = 10;
$countToPickByRow = 3;

if ($countToPickByRow > count($array)) {
    throw new \Exception('Impossible');
}

$rows = [];
for($i = 0; $i <= $countRows; $i++) {
    $row = [];

    while ($countToPickByRow !== count($row)) { 
        $random = $array[array_rand($array)];
        if (!in_array($random, $row)) { // If random value is'nt present, let's add it to my row
            $row[] = $random;
        }
    }

    $rows[] = $row; // Push generated row to my result rows array
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.