使用fgets |时创建array_reverse文本文件

时间:2018-02-08 04:17:27

标签: php arrays explode fgets array-reverse

我想将我的数组从最后一行重新排序到顶部的第一行。我尝试使用array_reverse,但是我的所有运行都失败了。

你们中的任何人都知道如何在我的代码中实现这一点? 一切正常,但我只需反向排列

由于

这是我的代码:

<?php
$f = fopen("list.txt", "r");

while (!feof($f)) { 

$arrM = explode("###",fgets($f));

echo "<p align='center'><b><font color='red' size='6'>" . $arrM[5]. " PLN</font></b><br><b><font size='5'>" . $arrM[3]. "</b></font><br><a target='_blank' href='" . $arrM[1] . "'><img src=" . $arrM[2]. " width='100%' /></a><br><br><br><br></p>";

}

fclose($f);
?>

1 个答案:

答案 0 :(得分:0)

试一下

 <?php
$data = [];

$f = fopen("list.txt", "r");

// store all data line by line in a 2D array
while (!feof($f)) { 
    $data[] = explode("###",fgets($f));
}

// reverse your data array
$arrM = array_reverse($data);

// echo data
foreach ($arrM as $row) {
    echo "<p align='center'>
    <b><font color='red' size='6'>" . $row[5]. " PLN</font></b><br>
    <b><font size='5'>" . $row[3]. "</b></font><br>
    <a target='_blank' href='" . $row[1] . "'><img src=" . $row[2]. " width='100%' /></a></p>";
}

fclose($f);  ?>