gpt4 book ai didi

php - 从文件中获取唯一的随机行并使用 php 将它们写入另一个文件

转载 作者:行者123 更新时间:2023-12-02 18:17:56 24 4
gpt4 key购买 nike

我的文件由 10000 行不同的行组成。我需要从此文件中取出 100 个随机 uniq 行并将它们写入另一个文件。使用 php 最简单的方法是什么?

最佳答案

一种天真的方式:

$lines = file('somefile.txt');
shuffle($lines);
$random_lines = array_slice($lines, 0, 10);

注意:这完全忽略了系统资源的考虑。

关于php - 从文件中获取唯一的随机行并使用 php 将它们写入另一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13421102/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com