开发者

Load file into PHP array, best way?

I have a text file that is around 1.7MB that has a list of words that I need for my application. I need to have it loaded into an array when the page loads and then be available to other PHP files that AJAX requests are made to. Right now I am loading it into an array on page load and then storing it in a session 开发者_JAVA百科so that the AJAX file has it available when requests are made to it. Is this the best way to do this?


Really depends on what you are doing with the data. If the AJAX only needs a certain couple of words from that huge list it would probably be best stored in a database.

Do remember that even if the list is stored in a session it will imply that you script loads up that array on every single call. Seams like a huge burden for something that maybe is not really needed.

You don't give us much detail, though.


If the entire array must be loaded into PHP, I'd write a script that made it into a PHP script that contains an array definition of the list of words, so you'd have a .php file that contains:

$myArray = array('blah', 'blah', 'blah'); //etc

Then, I'd include that (require_once)... most PHP script engines will be able to optimize that pretty easily... not much point in storing it in a session, would just use up more disk space/DB space/memory for storage.


The file command is pretty quick, so I'm sure there's going to be much to gain by caching anything.

Do you use the data on every single page load? If not, don't go with sessions as you'll have the overhead of loading it even when you don't need it.

Does the data depend upon the user? If not, don't go with session as you'll be duplicating the data all over the place for no reason.

If you are doing more complex parsing, then I'd suggest caching the file using

file_put_contents($filename, '<?php return '.var_export($array, true).';');

Then, all you need to do is $data = include $filename to fetch the data...

You could even make it smart enough to detect changes:

function getData() {
    static $data = array();
    if (empty($data)) {
        $cache = '/path/to/cache/file';
        $full = '/path/to/original/file';
        if (!file_exists($cache) || filemtime($cache) < filemtime($full)) {
            $data = parseDataFileIntoArray($full);
            $code = '<?php return '.var_export($data, true).';';
            file_put_contents($cache, $code);
        } else {
            $data = include $cache;
        }
    }
    return $data;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜