More than one way to skin a cat :)  The question obviously is whether or not
you need to read this data back or if it will be static, unread data.  You
can use several forms here, including the one below.  However, you can use
for, while and foreach loops to handle it:

/*  for (...) method */
$fp = fopen("/path/to/file", "w");  // open file for writing
for ($i = 0; $i < sizeof($array); $i++) {
    $status = fwrite($fp, $array[$i]);  // Change the part with $array to
whatever you need to write
}
$status = fclose($fp);


/*  foreach (...) method */
$fp = fopen("/path/to/file", "w");  // open file for writing
foreach ($array as $item) {
    $status = fwrite($fp, $item);  // Change the part with $item to whatever
you need to write
}
$status = fclose($fp);


/*  while(...) method */
$fp = fopen("/path/to/file", "w");  // open file for writing
while (list($key, $val) = each($array)) {
    $status = fwrite($fp, $val);  // Change the part with $val to whatever
you need to write
}
$status = fclose($fp);

There are other ways, as there always are with PHP.  However, these should
get you by in just about any circumstance.

Mike Frazer




"Greg Schnippel" <[EMAIL PROTECTED]> wrote in message
[EMAIL PROTECTED]">news:[EMAIL PROTECTED]...
Scott -

You can just write it as a pipe-delimited file, write it
to the file, and then read it back in. Use implode and explode:

$company = array("Item 1", "Item 2", "Item 3", "Item 4");

#---------------------------------------------
#
# Output array
#

$output_string = implode("|", $company);

<< write $output_string to a file >>

#
# Read array from file
#

if (!$file=fopen("file.txt", "r")) {
echo "Error opening file";
} else {
$input_string = fread($file,2048);
}

$company = explode("|", $input_string);

#---------------------------------------------

-schnippy

> -----Original Message-----
> From: Scott Saraniero [mailto:[EMAIL PROTECTED]]
> Sent: Tuesday, January 29, 2002 12:34 PM
> To: [EMAIL PROTECTED]
> Subject: [PHP] Write an array to a file
>
>
> Hi,
>
> How do I write an array to a file? For example, I need to
> write just this in
> a file:
> <?php $Company = array("Item 1", "Item 2", "Item 3", "Item
> 4", "Item 5",);
> ?>
>
> I've been hung up for 2 days on this. Any help would be appreciated.
>
> Thanks,
> Scott
>
>
> --
> 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]

Reply via email to