Forum Moderators: coopster

Message Too Old, No Replies

Array/Text file problem

Trying to save an array to textfile...

         

familyman

7:23 pm on Apr 26, 2004 (gmt 0)

10+ Year Member



Thanks so much for looking at this for me.

I am needing to keep a list of priorities that will need to be resorted often. Quite often I will need to add something to the middle of the list and bump others up.

I though the easiest way to do this would be to put them in a text file. I was able to read out of a text file with "$content = file($dbfile);".

I am now able to play with the order of the array.

I then tried to save this back to the text file by imploding using "\n" as a delimiter. This worked and I though my problem was solved.

After testing for a bit, I realized that while adding and moving entries, I would end up with a blank line in the middle of my text file. When I would continue to add entries it would compound the problem and I could end up with many blank lines.

I have butchered the code so much by now that It doesn't work at all now.

I guess my questions would be:

Is this the best way to approach this problem?
and if so, what method could I use to strip out any empty fields in my array before filing it?
and what is the best way to save an array to a file?

Thanks again.

chaplin

8:21 pm on Apr 26, 2004 (gmt 0)

10+ Year Member



Hello,

I would say using an array is a good desision if you work wih text file.
I would rather prefer using database but you may have reasons for text file.
To escape empty lines you can just write your simpel function replacing implode:

foreach($rows as $row)
{
if(empty($row)) continue;
$content.="$row\n";
}

and you have your new $content.
Hope i am making enough sense.

Chaplin.

Netizen

10:18 am on Apr 27, 2004 (gmt 0)

10+ Year Member



Take a look at the array_filter [php.net] function. An example to strip out blanks would be

$new=array_filter($old,'strip_blanks');

function strip_blanks($var) {
return!empty($var);
}

[Warning: untested code!]