Connecting Tech Pros Worldwide Forums | Help | Site Map

Writing an array to a text file

Newbie
 
Join Date: Feb 2007
Posts: 30
#1: Feb 28 '07
I have a function in my program to write an array to a text file, but every way I've tried results in the same result: when I open the file to check what's been written to it, all it displays is the word "Array" at the top. It's the last step I need to finish this assignment! I've tried a foreach loop, a listeach loop, and the simplest solution, shown below:

[PHP]function saveData($myFile)
{


if($file = fopen("employees.dat", "w")):
$numElements = count($myFile);
for($i = 0; $i < $numElements; $i++)
{
fwrite($file, $myFile[$i]);
}
fclose($file);

else:
echo("Error in opening file. Please re-submit");
echo("<A HREF ='gradedLab3.htm'>Return to form</A>");

endif;
}[/PHP]

None of these has helped. Any advice?

cassbiz's Avatar
Familiar Sight
 
Join Date: Oct 2006
Location: Florida
Posts: 204
#2: Feb 28 '07

re: Writing an array to a text file


If all you are writing is "Array" I would look at that part of the script to see if that is all you are asking it to write there.

I know that you have done a lot of work but the output being the word "Array" makes me think that you want to look at that part.

Can you show your entire script? It may help us better to help you.

Good Luck.
Newbie
 
Join Date: Feb 2007
Posts: 30
#3: Feb 28 '07

re: Writing an array to a text file


Thanks for the feedback. However, I'd already tested the code before posting this, and without going into too much detail, it is definitely passing the array properly into the function. (I used the print_r() method to display it, so I know for sure). The problem seems to be that the fwrite method doesn't work for arrays, only strings, even though each element of the array IS a string. Are there any special methods for writing an array? I've already tested everything else and it all works except the last function. (By the way, ignore most of the comments; they were part of the code to start with).

And, here's the complete code:

?php
/*
loadData: returns an array of lines in a file
Parameters: the name of the file to be loaded, a string
the default is "employees.dat"
Returns: the array of lines in that file
*/

//write loadData here
function loadData($defaultFileName = "employees.dat")
{
if(!$defaultFileName = file("employees.dat"))
{
echo('Error while opening file');
}

return $defaultFileName;
}



/*
searchData: searches an array for a string
Parameters: $myFile - the array to look through
$oldName - the string to look for
Returns: the index where the string is found in the array or -1 if not found
*/

//write searchData here
function searchData($myFile, $oldName)
{
$a = $myFile;
$s = $oldName;

$numElements = count($myFile);
for($i = 0; $i < $numElements; $i++)
{
if((eregi($s, $a[$i])))
{
return $i;
}
else
{
if($i == $numElements-1)
{
return -1;
}
else
{
$i++;
}
}
}

}


/*
updateData: updates lines in an array
Parameters: $a - the array
$i - index in the array to update
$sOld - name in string to replace
$sNew - name to replace $sOld with
*/

//write updateData here
function updateData($myFile, $index, $oldName, $newName)
{
$a = $myFile;
$i = $index;
$sOld = $oldName;
$sNew = $newName;

$a[$i] = eregi_replace($sOld,$sNew, $a[$i]);
return array($a);

}



/*
saveData: saves array to a file, does not save empty array elements
Parameters: $a - array to be saved
$defaultFilename - name of file, default value "employees.dat"
*/
//write saveData here
function saveData($myFile)
{
echo("<br>");
print_r($myFile);
echo("<br>");

if($file = fopen("employees.dat", "w")):
$numElements = count($myFile);
for($i = 0; $i < $numElements; $i++)
{
fwrite($file, $myFile[$i]);
}
fclose($file);

else:
echo("Error in opening file. Please re-submit");
echo("<A HREF ='gradedLab3.htm'>Return to form</A>");

endif;
}






if(!empty($newName)):
$myFile = loadData();
$index = searchData($myFile,$oldName);
if($index == -1):
echo("<HTML><HEAD><TITLE>$name not found</TITLE></HEAD><BODY>");
echo("<B>$name</B> not found <BR>");
echo("<A HREF ='gradedLab3.htm'>Return to form</A>");
echo("<B>$name</B></HTML>");
exit();
else:
//update name and save data
//call updateData here

$myFile = updateData($myFile, $index, $oldName, $newName);
saveData($myFile);

?>

<HTML>
<HEAD>
<TITLE>Employee Updated</TITLE>
</HEAD>
<BODY>
<B><?php echo($oldName); ?></B> was updated <?php echo("to $newName"); ?><BR>
<A HREF = "gradedLab3.htm">Return to Form</A>

</BODY>
</HTML>
<?php
endif;
else:

?>

<HTML>
<HEAD>
<TITLE>No Name</TITLE>
</HEAD>
<BODY>
<B>No name was submitted</B><BR>
<A HREF = "gradedLab3.htm">Return to Form</A>

</BODY>
</HTML>

<?php

endif;
?>
Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#4: Feb 28 '07

re: Writing an array to a text file


The function you are looking for is var_export().
Newbie
 
Join Date: Feb 2007
Posts: 30
#5: Feb 28 '07

re: Writing an array to a text file


I checked that one... my issue is that I need this to write to a file instead of displaying on the screen. How would I go about doing that using this method?
Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#6: Feb 28 '07

re: Writing an array to a text file


Quote:

Originally Posted by tnspc

I checked that one... my issue is that I need this to write to a file instead of displaying on the screen. How would I go about doing that using this method?

Assign a variable to var_export() and then write the variable to a file.
Newbie
 
Join Date: Feb 2007
Posts: 30
#7: Feb 28 '07

re: Writing an array to a text file


Code:


$file = fopen("employees.dat", "w");
fwrite($file, var_export($myFile));
fclose($file);

resulted in a blank "employees.dat" file and displayed the original array as associated with : array[0]=>.
Motoma's Avatar
Moderator
 
Join Date: Jan 2007
Location: Maine, USA
Posts: 2,904
#8: Feb 28 '07

re: Writing an array to a text file


Quote:

Originally Posted by tnspc

Code:


$file = fopen("employees.dat", "w");
fwrite($file, var_export($myFile));
fclose($file);

resulted in a blank "employees.dat" file and displayed the original array as associated with : array[0]=>.

It appears, then, that $myFile is an array with one element which is empty.
Newbie
 
Join Date: Nov 2007
Posts: 5
#9: Nov 5 '07

re: Writing an array to a text file


I was also trying to use var_export for arrays. What about this:
[PHP]

$FileName = "Some_File.txt";
$Data = file($FileName);
$Lines = count($Data);
...
for ($i=0; $i<$Lines; $i++){
$Array .= "$Data[$i]\n" ; // !!
}

[/PHP]
now you can write the $Array using fwrite() without problems.
Newbie
 
Join Date: Feb 2009
Posts: 1
#10: Feb 10 '09

re: Writing an array to a text file


Quote:

Originally Posted by tnspc View Post

Code:


$file = fopen("employees.dat", "w");
fwrite($file, var_export($myFile));
fclose($file);

resulted in a blank "employees.dat" file and displayed the original array as associated with : array[0]=>.

Set the second arg to var_export to 1. This tells var_export to return its results as a string instead of writing to the terminal.

Expand|Select|Wrap|Line Numbers
  1. $file = fopen("employees.dat", "w");
  2. fwrite($file, var_export($myFile,1));
  3. fclose($file);
This worked for me
Reply