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()????

5
  • I've read your question 5 times now and I still don't understand what you want to achieve. Commented Apr 9, 2012 at 19:00
  • check last paragraph of question Commented Apr 9, 2012 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. Commented Apr 9, 2012 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 ); Commented Apr 9, 2012 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() Commented Apr 9, 2012 at 19:30

4 Answers 4

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');

Sign up to request clarification or add additional context in comments.

3 Comments

I like that. Simple but again, not really what im looking for.
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...
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.
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

1 Comment

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
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.

1 Comment

In append mode fseeks behavior is undefined. Re-read the link you have given
-1

Untested. Try this:

$data = file_get_contents('./data.txt', true);
$array = eval("array(".$data.")");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.