Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从文本文件中获取随机行_Php_Arrays_File - Fatal编程技术网

Php 从文本文件中获取随机行

Php 从文本文件中获取随机行,php,arrays,file,Php,Arrays,File,当用户刷新页面时,我想从random.txt调用随机词,但我得到的是$word\u数组[0],而不是任何其他$word\u数组[1..3] random.txt hello how are you PHP代码: myfile = fopen("random.txt", "r") or die("Unable to open file!"); $word_array = array(fgets($myfile)); $word = rand(0,4); $lines[] = fgets($my

当用户刷新页面时,我想从
random.txt
调用随机词,但我得到的是
$word\u数组[0]
,而不是任何其他$word\u数组[1..3]

random.txt

hello
how
are
you
PHP代码:

myfile = fopen("random.txt", "r") or die("Unable to open file!");
$word_array = array(fgets($myfile));

$word = rand(0,4);
$lines[] = fgets($myfile);
echo $word_array[$word];

fclose ($myfile); 
错误是什么


更新:如果可以避免循环,请仅更正此代码。

代码中的问题是,您只将文件的第一行放入数组中:

$word_array = array(fgets($myfile));
意味着您在这里拥有的是:

Array (
    [0] => First line
)
因此,如果您已打开,您将收到通知:

注意:未定义的偏移量

75%的时间

但要想做你想做的事情,你只需要将你的文件放入一个数组中,并与


FGET只能逐行读取。 所以你需要一些类似的东西:

$lines[] = fgets($myfile);
$lines[] = fgets($myfile);
$lines[] = fgets($myfile);
$lines[] = fgets($myfile);
echo $lines[$word];

4行4次

短:fgets只读取一行 您需要创建一个while语句来读取所有行

因此:

有趣的问题=)你可以试试我的解决方案:

$myfile = fopen("random.txt", "r") or die("Unable to open file!");
$words = fgets($myfile);//gets only first line of a file
$num = str_word_count($words) - 1 ;//count number of words, but array index
//starts from 0, so we need -1
$word_array = explode(' ', $words);//gets all words as array

$word = rand(0,$num);//getting random word number
echo $word_array[$word];

fclose ($myfile); 
如果需要所有行,则必须使用while循环迭代所有行

while($row = fgets($myfile)){
    //same code
}

这不是给你一个偏移错误吗?
fgets
只获取一行,而不是整个文件。
file()
解决方案是正确的
$myfile = fopen("random.txt", "r") or die("Unable to open file!");
$words = fgets($myfile);//gets only first line of a file
$num = str_word_count($words) - 1 ;//count number of words, but array index
//starts from 0, so we need -1
$word_array = explode(' ', $words);//gets all words as array

$word = rand(0,$num);//getting random word number
echo $word_array[$word];

fclose ($myfile); 
while($row = fgets($myfile)){
    //same code
}