开发者

PHP: Writing and sorting a file

I'm trying to write a php function that takes the $name and $time and write it to a txt file (no mySQL) and sort the file numerically.

For example:

10.2342 bob
11.3848 CandyBoy
11.3859 Minsi
12.2001 dj

just added Minsi under a faster time, for example.

If the $name already exists in the file, only rewrite it if the t开发者_运维技巧ime is faster (smaller) than the previous one, and only write if the time fits within 300 entries to keep the file small.

My forte isn't file writing but I was guessing to go about using the file() to turn the whole file into an array, but to my avail, it didn't work quite like I wanted. Any help would be appreciated


If your data sets are small, you may consider using var_export()

function dump($filename, Array &$data){
    return file_put_contents('<?php return ' . var_export($data, true) . ';');
}

// create a data set
$myData = array('alpha', 'beta', 'gamma');

// save a data set
dump('file.dat', $myData);

// load a data set
$myData = require('file.dat');

Perform your sorts using the PHP array_* functions, and dump when necessary. var_export() saves the data as PHP parsable text, which is why the dump() function prepends the string <?php return. Of course, this is really only a viable option when your data sets are going to be small enough that keeping their contents in memory is not unreasonable.


Try creating a multi dimensional array "$timeArray[key][time] = name" and then sort($timeArray)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜