generate a random array of numbers in php

To generate a random array of numbers in PHP, we can use a combination of range and shuffle functions.

range function is used to create an array containing a range of elements. We can pass the starting and ending value of the range as arguments, and it will return an array with all the elements in between.

shuffle function is used to randomly shuffle the elements of an array.

Here's an example of how to generate a random array of numbers in PHP:

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

// Shuffle the array
shuffle($numbers);

// Print the shuffled array
print_r($numbers);
?>
168 chars
11 lines

The output of the above code will be an array of numbers in random order, like this:

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

gistlibby LogSnag