PHP;分配fgets()输出到一个数组 [英] PHP; assigning fgets() output to an array

查看:226
本文介绍了PHP;分配fgets()输出到一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将PHP $中的 fgets()函数返回给一个数组。我已经尝试过测试字符串,他们工作正常。我还确认 fgets()正在返回项目,但仍然没有快乐。考虑到这可能是一个计时问题,我有函数运行 onload ,这是行不通的。我的代码在下面;

$ p $函数createDataArray()

global $ resultsArray;

$ i = 0;
$ file = fopen(downloads / E0.csv,r); $!
$ b while(!feof($ file))
{
$ line = fgets($ file,4096);
$ resultsArray [$ i] = $ line; //这不行。亦范范辛读方亦预这是一个字符串,但它不会被分配给数组。
$ i = $ i + 1;
}
fclose($ file);


解决方案



函数createDataArray()
{
$ resultsArray = array();

$ file = fopen(downloads / E0.csv,r); $!
$ b while(!feof($ file))
{
$ line = fgets($ file,4096);
$ resultsArray [] = $ line;
}
fclose($ file);

return $ resultsArray;
}


I am attempting to assign the string returned by the fgets() function to an array in PHP. I have tried test strings and they work fine. I have also made sure that fgets() is returning items, but still no joy. Thinking that it may be a timing issue, I had the function run onload and that didn't work. My code is below; any help on this would be much appreciated.

function createDataArray()
    {
        global $resultsArray;

        $i = 0;
        $file = fopen("downloads/E0.csv","r");

        while(! feof($file))
        {
            $line = fgets($file, 4096);
            $resultsArray[$i] = $line; //This isn't working. Something is wrong with $line. It is a string, but it doesn't get assigned to the array.
            $i = $i + 1;
        }
        fclose($file);
    }

解决方案

PLEASE return the array; do not use globals.

This fix should work:

function createDataArray()
    {
        $resultsArray = array();

        $file = fopen("downloads/E0.csv","r");

        while(! feof($file))
        {
            $line = fgets($file, 4096);
            $resultsArray[] = $line; 
        }
        fclose($file);

        return $resultsArray;
    }

这篇关于PHP;分配fgets()输出到一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆