How to get the data of the first row randomly by php

there are four rows of data
1111111
2222222
3333333
4444444

I"m going to randomly pick any row and assign it to a variable. How does php implement

?
Php
Aug.22,2021

doesn't know where your four rows of data are stored. Random number generation is generated using rand or mt_rand .


$dataArr = [
    1111111,
    2222222,
    3333333,
    4444444,
];

$random = $dataArr[array_rand($dataArr)];

$random is one of the data obtained at random.


turn it into an array:
$arr = [
1111111,
222222,
3333333,
4444444
];
then use count ($arr) to calculate the length, and then use $key = rand (0, count ($arr)-1) to randomly go to a key,$arr [$key] that is the

you want.

complete:
$arr = [
111111,
222222,
3333333,
4444444
];

$key = rand (0, count ($arr)-1)
$arr [$key]

there is another way:
scramble the array with shuffle (), and then take the first one.


$data = [1111111,2222222,3333333,4444444];

$max = count($data) - 1;

$rand = $data[rand(0,$max)];

$data=file_get_contents("test.txt");
$data_url=explode("\r\n",$data);
$data_url[1] 

got it

Menu