0

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);
    }
Kevin
  • 53,822
  • 15
  • 101
  • 132
barryedmund
  • 3
  • 2
  • 4

1 Answers1

3

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;
    }
Brian Driscoll
  • 19,373
  • 3
  • 46
  • 65
  • Thank you for the help, but is that the fundamental issue here? I'm not sure that it is (see my comment to zerkms above). I have done a bit of a hacky workaround using this code `$resultsArray[$i] = substr($line, 0, 250);` and this puts the first 251 characters into the array. Obviously, this is not great code and would I like to get to the bottom of the issue. Any thoughts? Barry – barryedmund Dec 12 '11 at 19:57
  • @user1094389: well, that is not possible. If assignment to `substr($line, 0, 250);` then assignment to `$line` should work either – zerkms Dec 12 '11 at 20:01
  • well, here's the thing - if you're using the same `$resultsArray` globally how can you know that a specific index of the array isn't being sliced or overwritten somewhere else in your application? – Brian Driscoll Dec 12 '11 at 20:03
  • @zerkms: That's what's confusing me so much! The total string length is ~350 chars. Is that too long? 251 characters doesn't work, but 250 does! – barryedmund Dec 12 '11 at 20:04
  • @BrianDriscoll: Because this is the only time I am writing to the array. Am I being completely stupid? – barryedmund Dec 12 '11 at 20:06
  • @user1094389: in php the string length is only limited by free memory amount. So, no, 350 is not much – zerkms Dec 12 '11 at 20:09
  • @user1094389 out of curiosity, is there a newline at `substr($line,0,250)`? It wouldn't explain why `$resultsArray[] = $line` doesn't work, but it would explain why `$resultsArray[] = substr($line,0,251)` doesn't work. – Brian Driscoll Dec 12 '11 at 20:11
  • @BrianDriscoll: No line breaks whatsoever. Anyhoo, it isn't a big deal; I have an end result that works and this is just a personal project anyway. Thanks for all your help. ps This is my first time posting on SO and it's great! – barryedmund Dec 12 '11 at 20:24