generate a random array in php

To generate a random array in PHP, you can use the range function to create an array with a sequence of numbers, and then use the shuffle function to randomize the order of the elements in the array. Here's an example code snippet:

main.php
<?php
// Generate an array with numbers from 1 to 10
$numbers = range(1, 10);

// Shuffle the array to randomize the order
shuffle($numbers);

// Print the random array
print_r($numbers);
?>
191 chars
11 lines

This code will output an array with numbers from 1 to 10 in random order, for example:

main.php
Array
(
    [0] => 9
    [1] => 4
    [2] => 8
    [3] => 3
    [4] => 2
    [5] => 5
    [6] => 10
    [7] => 6
    [8] => 1
    [9] => 7
)
141 chars
14 lines

gistlibby LogSnag