我這裏有我的php代碼和txt文件。我想在php的輸出中隨機顯示6行.txt文件。我該怎麼做。我嘗試我的代碼,但它只顯示前6行。 謝謝!PHP顯示前6行txt文件
PHP的:
require_once "config.php";
$txt_file = file_get_contents($database);
$rows = explode("\n", $txt_file);
array_shift($rows);
$i = 0;
foreach($rows as $row => $data)
{
$row_data = explode('&id=', $data);
$info[$row]['name'] = $row_data[0];
$info[$row]['id'] = $row_data[1];
$name = $info[$row]['name'];
$id = $info[$row]['id'];
echo $name."<BR>";
echo $id."<BR><BR>";
if (++$i == "6") break;
}
txt文件:
ABC&id=1
DEF&id=2
GHI&id=3
嘗試洗牌數組,然後運行循環? http://php.net/manual/en/function.shuffle.php – Salman