2011-05-07 73 views
1

我有一個使用natsort(排序的文件)倒車使用PHP

一個文本文件...(按升序)但實際上我想它降序排列..

我的意思文件的最後一行必須第一線,反之亦然

請讓我知道是否有任何功能或片段來實現這一目標..

我不是那種擅長PHP,不論欣賞質量的所有響應...謝謝

+3

'file()'+'array_reverse()'+'implode()'+'file_put_contents()' – zerkms 2011-05-07 13:25:01

+0

http://goo.gl/dX8QT – 2011-05-07 13:32:40

回答

0

array_reverse會給內容按降序排列

$reverse = array_reverse($array, true); 
0

雖然不是一個大的文本文件最有效的方法,你可以使用filearray_reversefile_put_contents實現這一目標如下...

<?php 
    // Fetch each line from the file into an array 
    $fileLines = file('/path/to/text/file.txt'); 

    // Swap the order of the array 
    $invertedLines = array_reverse($fileLines); 

    // Write the data back to disk 
    file_put_contents('/path/to/write/new/file/to.txt', $invertedLines); 
?> 

...實現你所追求的。