Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to save multiple php arrays one at a time. The arrays come to me from a parser function also one at a time. I used to keep them all in memory and then write them all at once like this:

while($onearr = parser())
     $allarr[] = $onearr;

  ..
  ..
  fwrite($filename,json_encode($allarr));

But this logic did not hold for long. I started running out of memory quickly. I want to write the arrays one at a time to the same file and read them one at a time too. This is my writer function:

function savearr($onearr) {        
    if($fp = fopen('arrFile.json','a+'))  {    
       $rc = fwrite($fp, json_encode($onearr));
       fclose($fp);
    }   
}

Now I cannot figure a way to read these arrays! Any way to do it? I tried reading the whole file at once but was not sure how to parse it correctly into individual arrays to match the original!

Thanks in advance

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Firstly, you may want to rethink your savearr() function, you're opening/closing the file each time you write the array. Perhaps a class note: none of this was tested:

class ArrayWriter {
  protected $fp;
  public function __construct() {
    $this->fp = fopen('arrFile.json', 'a+');
    if(!$this->fp) die("Unable to open file");
  }

  public function __destruct() {
    if($this->fp) fclose($this->fp);
  }

  public function write($array = array()) {
    if($this->fp) fwrite($this->fp, json_encode($array) . "\n");
  }
}

Now that we've cleared that up, we can do the reverse to read:

function read() {
  $arrays = file('arrFile.json');
  $result = array();
  foreach($arrays as $line)
    $result[] = json_decode($line);
  }
  return $result;
}
share|improve this answer
    
Thank you very much! I knew I was missing something very simple which was adding "\n" to fwrite of the each json record so I can read it back with fgets(). This worked right away. The only thing I had to fix was that the file always had an extra null record which resulted in an extra null array in the array list that I had to clean up after the full file read. –  seedhom May 31 '13 at 20:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.