[PHP] Writing Array to File w/o blank lines

2002-01-13 Thread Andrew V. Romero

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; $icount($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


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] Writing Array to File w/o blank lines

2002-01-13 Thread Philip Olson

Hello Andrew-

This can be solved here or better yet, solved from the place that removes
the array elements.  Use unset() for that:

  unset($arr[$key]);

Now, when you loop through the array it'll have no empty elements.  
Rather then use for(), consider foreach() as it relies on no counts.  
But, in this case, I'd most likely create a string first then fwrite()
just once.  Something like:

  $data = implode(\n, $array);

Also, consider renaming the function/vars to something a bit more generic,
such as createFile or createFileFromArray but anyway, just an opinion :)  
And, use fclose().  Also for good measure, you may want to check if $file
is_writable() first.

Regards,
Philip Olson

p.s. Please keep questions to one thread.


On Sun, 13 Jan 2002, Andrew V. Romero wrote:

 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; $icount($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
 
 
 -- 
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 To contact the list administrators, e-mail: [EMAIL PROTECTED]
 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]