2

I am having trouble figuring out how to write to a file using fopen "a" append mode.

the file itself is a simple PHP array:

$array = array(
  "entry1"   => "blah blah",
  "entry2"   => "forbarbaz",
);   

simple enough. So using fopen with the 2nd arg set to "a" should allow me to append the file using fputs.... the problem is the opening and closing lines, ie $array = array( and );

so now the file should look like this:

  "entry1"   => "blah blah",
  "entry2"   => "forbarbaz",

how would I rebuild this data into a working PHP array assuming it is just a txt file with a list of entries without the opening and closing lines? Sorry if this is not clear, its a little complicated. No I am not going to store these values in a DB, I need the speed advantage by holding these particular values in a file array.

So the questions really is how would i go about constructing the usable PHP array from a txt file with a line by line list like this?

To clarify:

how do i pull in a txt file with lines like this:

  "entry1"   => "blah blah",
  "entry2"   => "forbarbaz",

and have a workable $php_array()????

Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108
Nick
  • 908
  • 12
  • 29
  • I've read your question 5 times now and I still don't understand what you want to achieve. – Simon Forsberg Apr 09 '12 at 19:00
  • check last paragraph of question – Nick Apr 09 '12 at 19:04
  • @Nick Seems to be impossible to implement what you need w/o additional headache. You cannot execute **invalid** php-code as php-code. So you must either work with file as with non-php file (i.e. `eval` it as suggested in one of answers, or read it somehow), or make it **valid** php-file as I've suggested. – Botanick Apr 09 '12 at 19:08
  • I don't think this is a good way to do this, but maybe you are looking for something like... file_put_contents('./inc.array.php', ' "entry1"=>"some value", '."\n", FILE_APPEND ); – Brian Bell Apr 09 '12 at 19:08
  • both good comments. Yes the issue with appending to an array before the closing ); I am now working along the lines of a txt file and using eval() – Nick Apr 09 '12 at 19:30

4 Answers4

3

Try this.

File format (at the beginning of work with it):

<?php
$array = array();

Now it's correct php-file.

Then simply add new rows like as follows:

$f = fopen('myarray.php', 'a');
fputs($f, PHP_EOL.'$array["entry1"] = "value1";');
fclose($f);

And use it by simply include('myarray.php');

Botanick
  • 663
  • 5
  • 10
  • I like that. Simple but again, not really what im looking for. – Nick Apr 09 '12 at 19:00
  • Can you explain in more detail what you are looking for? Botanick's solution will give you a file with an array of values that you can reference after you have included that file into another document. Not sure what else you need based on your initial description... – Brian Bell Apr 09 '12 at 19:03
  • as this was the closest to the solution, i am going to award the answer to Botanick.. In the end though I decided to read in the current array values and re-write the entire array. Although this answer did allow me to append items to the array in the static file, it lead to allot of duplication. – Nick Apr 12 '12 at 21:26
0

Why not json_encode the array when you store it in the file and then json_decode the JSON into an array when you extract it from the file?

json_encode
json_decode

chapkom
  • 54
  • 1
  • 3
  • no, I am not looking to change the data storage mechanisim, infact JSON is a step in the wrond direction, and does not solve the "append" issue. This file is going to be constantly updated as time goes by - its going to hold some language translation values. I dont care how server heavy the creation of this array is, but reading it must be as fast as possible – Nick Apr 09 '12 at 18:57
0

Maybe you're looking for the fseek function: http://se.php.net/fseek

When opening a file in r+ mode, the file pointer is at the beginning of the file. Sounds like you want to place it at the end of the file minus a few bytes, then write your new data.

Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108
-1

Untested. Try this:

$data = file_get_contents('./data.txt', true);
$array = eval("array(".$data.")");
John Conde
  • 217,595
  • 99
  • 455
  • 496