Home > Backend Development > PHP Tutorial > 数组随机排序怎么随机排序

数组随机排序怎么随机排序

WBOY
Release: 2016-06-23 13:34:04
Original
1066 people have browsed it

在1.2.3.4文本里面随机选择一行,然后组合成一句话,比如:
去干嘛,去吃饭,太好了,么么哒
或者
太好了,吃饱了,做什么,开心


回复讨论(解决方案)

$filenames=array('1.txt','2.txt','3.txt','4.txt');$str='';foreach($filenames as $k=>$file){	$tmp=file_get_contents($file);	$data[$k]=explode(PHP_EOL,$tmp);	$str.=$data[$k][array_rand($data[$k])].',';}echo $str;//太好了,吃饭了,去干嘛,谢谢,
Copy after login
Copy after login

$filenames=array('1.txt','2.txt','3.txt','4.txt');$str='';foreach($filenames as $k=>$file){	$tmp=file_get_contents($file);	$data[$k]=explode(PHP_EOL,$tmp);	$str.=$data[$k][array_rand($data[$k])].',';}echo $str;//太好了,吃饭了,去干嘛,谢谢,
Copy after login
Copy after login


测试了,你这个排序是顺序的,比如说从1.txt读取一行,接着2.txt读取一行,接着3.txt读取一行,4.txt读取一行组成的一句话。
我想要的效果是随机从1.2.3.4文本读取一行组成一句话。

在 $filenames=array('1.txt','2.txt','3.txt','4.txt'); 下面加上这一句
shuffle($filenames);

在 $filenames=array('1.txt','2.txt','3.txt','4.txt'); 下面加上这一句
shuffle($filenames);

非常感谢哦,我是个纯新手,稍微懂一点,但是不知道php的写法和顺序,反正特别累
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template