$file1 = file("1.txt"); //按行读取1.txt数据,存到数组$file1里 //var_dump($file1); $file2 = file("2.txt"); $file3 = ""; //初始化$file3 $len = sizeof($file2); //file2的长度(也就是行数) //var_dump($len);die; foreach($file1 as $f){ //循环$file1 $f = trim($f); $i = mt_rand(0,$len-1); //随机获取一个0到$file2长度-1的数 $f2 = $file2[$i]; //获取$file2的一条数据(某一行) $f2 = trim($f2); $file3 .= $f.",".$f2." "; //拼接字符串 } if(file_put_contents("3.txt",$file3)){ //将$file3写入文件3.txt echo "写入成功"; }else{ echo "失败"; }