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

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1eb23c5-23ba.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1eb23c5-23ba.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?