Unable to write an array to a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tejas900
    New Member
    • Jul 2007
    • 1

    #1

    Unable to write an array to a file

    hi All,

    I am extracting data from a website using a automation tool. I am getting the data in the form of an array, which I want to write it in a text file. Please have a look at the below piece of code which is not working. it creates a file but just write a word 'Array' into it instead of the data.

    $rec = split("\[EXTRACT\]", $e);

    echo "$rec";
    //inserting the extracted data in to text file
    $myFile = "testFile.t xt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh, $rec);
    fclose($fh);

    Any quick help is really appreciated.
  • kovik
    Recognized Expert Top Contributor
    • Jun 2007
    • 1044

    #2
    And what seems to be the problem?

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, tejas900. Welcome to TSDN!

      When you try to print() an array, it will output as 'Array'. Not too helpful. What you want is var_export() instead.

      Comment

      • kovik
        Recognized Expert Top Contributor
        • Jun 2007
        • 1044

        #4
        A more common approach is print_r().

        You'd likely want to actually traverse your arrays and add each index into the file.
        [php]foreach($rec as $data)
        {
        echo $data;
        }[/php]

        That should give you an idea of how it's done.

        Comment

        Working...