Writing Array to File w/o blank lines
| From: | Andrew V. Romero | Date: | Mon, 14 Jan 2002 00:43:41 +0000 |
| Subject: | Writing Array to File w/o blank lines | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-80424@lists.php.net to get a copy of this message | ||
I have been working on a script that manipulates arrays and now I am
working on the part where the user can erase part of the array.
Previously the function that writes the array to a file looked like:
function updateSuspenderOrVehicleFile($fileToUpdate, $newDataArray)
{
$fileName = fopen( $fileToUpdate,"w");
for ($i=0; $i<count($newDataArray); $i++)
{
//don't write blank lines left by remove feature
if ($newDataArray[$i] != "")
{
//check to see if on the last solution, if not add line break at end.
if ($i+1 == count($newDataArray) || $newDataArray[$i +1] == "")
fwrite($fileName,"$newDataArray[$i]");
else
fwrite($fileName,"$newDataArray[$i]\r\n");
}
}
}
The problem is that when a user erases part of the array, I just set it
to array[$number] == "", which works except in the following cases.
Lets say the user selects the last 2 array elements to erase, then when
the function is called to write the array, it leaves a couple blank
lines at the end of the file which messes up things later. Originally I
though I would be easier to set the record they want to erase to "", but
is there another way to say compress the whole array together or a
better way to do this?
Thanks for any ideas,
Andrew V. Romero