By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,612 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

Writing an array to a text file

P: 30
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?
Feb 28 '07 #1
Share this Question
Share on Google+
10 Replies


cassbiz
100+
P: 202
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.
Feb 28 '07 #2

P: 30
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:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /* 
  3.    loadData: returns an array of lines in a file
  4.    Parameters: the name of the file to be loaded, a string
  5.            the default is "employees.dat"
  6.    Returns: the array of lines in that file
  7. */
  8.  
  9. //write loadData here
  10.    function loadData($defaultFileName = "employees.dat")
  11.   {
  12.      if(!$defaultFileName = file("employees.dat"))
  13.        {
  14.         echo('Error while opening file');
  15.        }
  16.  
  17.         return $defaultFileName;
  18.   }   
  19.  
  20.  
  21.  
  22. /*
  23.    searchData: searches an array for a string
  24.    Parameters: $myFile - the array to look through
  25.                $oldName - the string to look for
  26.    Returns: the index where the string is found in the array or -1 if not found
  27. */
  28.  
  29. //write searchData here
  30. function searchData($myFile, $oldName)
  31.      {
  32.     $a = $myFile;
  33.     $s = $oldName;
  34.  
  35.     $numElements = count($myFile);
  36.       for($i = 0; $i < $numElements; $i++)  
  37.         {
  38.          if((eregi($s, $a[$i])))
  39.         {
  40.         return $i;
  41.                 }
  42.          else
  43.         {
  44.          if($i == $numElements-1)        
  45.                   {
  46.               return -1;
  47.                     } 
  48.             else
  49.                {
  50.               $i++;
  51.                   }  
  52.                 }
  53.         }
  54.  
  55.       }
  56.  
  57.  
  58. /*
  59.    updateData: updates lines in an array
  60.    Parameters: $a - the array
  61.            $i - index in the array to update
  62.            $sOld - name in string to replace
  63.            $sNew - name to replace $sOld with
  64. */
  65.  
  66. //write updateData here
  67.     function updateData($myFile, $index, $oldName, $newName)
  68.         {
  69.          $a = $myFile;
  70.          $i = $index;
  71.          $sOld = $oldName;
  72.          $sNew = $newName;
  73.  
  74.          $a[$i] = eregi_replace($sOld,$sNew, $a[$i]);          
  75.              return array($a);
  76.  
  77.         }
  78.  
  79.  
  80.  
  81. /*
  82.    saveData: saves array to a file, does not save empty array elements
  83.    Parameters: $a - array to be saved
  84.            $defaultFilename - name of file, default value "employees.dat"
  85. */
  86. //write saveData here
  87.     function saveData($myFile)
  88.         {
  89. echo("<br>");
  90. print_r($myFile);        
  91. echo("<br>");
  92.  
  93.         if($file = fopen("employees.dat", "w")):
  94.            $numElements = count($myFile);
  95.            for($i = 0; $i < $numElements; $i++)
  96.            {            
  97.                     fwrite($file, $myFile[$i]);
  98.            }    
  99.             fclose($file);    
  100.  
  101.         else:
  102.            echo("Error in opening file. Please re-submit");
  103.            echo("<A HREF ='gradedLab3.htm'>Return to form</A>");        
  104.  
  105.         endif;
  106.         }
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.    if(!empty($newName)):
  114.     $myFile = loadData();
  115.         $index = searchData($myFile,$oldName);
  116.     if($index == -1):
  117.         echo("<HTML><HEAD><TITLE>$name not found</TITLE></HEAD><BODY>");
  118.         echo("<B>$name</B> not found <BR>");
  119.         echo("<A HREF ='gradedLab3.htm'>Return to form</A>");
  120.             echo("<B>$name</B></HTML>");
  121.         exit();
  122.     else: 
  123.          //update name and save data
  124.          //call updateData here
  125.  
  126.            $myFile = updateData($myFile, $index, $oldName, $newName);
  127.            saveData($myFile);
  128.  
  129. ?>
  130.  
  131. <HTML>
  132. <HEAD>
  133. <TITLE>Employee Updated</TITLE>
  134. </HEAD>
  135. <BODY>
  136. <B><?php echo($oldName); ?></B> was updated <?php echo("to $newName"); ?><BR>
  137. <A HREF = "gradedLab3.htm">Return to Form</A>
  138.  
  139. </BODY>
  140. </HTML>
  141. <?php
  142.     endif;
  143.     else:
  144.  
  145. ?>
  146.  
  147. <HTML>
  148. <HEAD>
  149. <TITLE>No Name</TITLE>
  150. </HEAD>
  151. <BODY>
  152. <B>No name was submitted</B><BR>
  153. <A HREF = "gradedLab3.htm">Return to Form</A>
  154.  
  155. </BODY>
  156. </HTML>
  157.  
  158. <?php
  159.  
  160.     endif;
  161. ?>
Feb 28 '07 #3

Motoma
Expert 2.5K+
P: 3,235
The function you are looking for is var_export().
Feb 28 '07 #4

P: 30
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?
Feb 28 '07 #5

Motoma
Expert 2.5K+
P: 3,235
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.
Feb 28 '07 #6

P: 30
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]=>.
Feb 28 '07 #7

Motoma
Expert 2.5K+
P: 3,235
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.
Feb 28 '07 #8

P: 5
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.
Nov 5 '07 #9

P: 1
@tnspc
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
Feb 10 '09 #10

P: 1
@Motoma
Kudos buddy. Worked well
May 3 '12 #11

Post your reply

Sign in to post your reply or Sign up for a free account.