Get uniq random lines from file and write them to another file using php
问题 I have file consists of 10000 different lines. I need to take 100 random uniq lines from this file and write them to another file. What is the easiest way to do it using php? 回答1: A naive way: $lines = file('somefile.txt'); shuffle($lines); $random_lines = array_slice($lines, 0, 10); Note: This completely disregards system resource considerations. 回答2: A faster solution larger lines function m1($file) { $fp = fopen($file, "r"); $size = filesize($file); $list = array(); $n = 0; while ( true )