Jump to content

morningside

New Members
  • Posts

    9
  • Joined

  • Last visited

    Never

Posts posted by morningside

  1. <?php

     

    echo "The contents of the file is " . file_get_contents('ArrayTestFile.txt') . "<br />";

    echo "<br />";

    $fh = fopen("ArrayTestFile.txt", 'r+') or die("Failure");

     

    $array = array(); //creation of array

     

    for ($j = 0; $j <= 3; $j++) //for loop loads first 5 of .txt

      {

        $num = fgets($fh);// This recursive call must be in the loop    $array[$j] = $num; //loads each line of .txt into the array at the loops index.

        echo "Line " . $j . " of the array is " . $array[$j] . "." . "<br />"; // displays the contents of the array

      }

      ?>

  2. Thanks Jcbones.

    But what if I wanted to call the contents of a line from the file again.

    Being out side of the for statment. How would I do this?

    Could I make the statment $line[0]; to return the 1 from the file?

     

    For example if the array was coded like this then I would be able to call elements of the array by the code

    echo $numbers[0];

     

    <?php

        $numbers = array('10', '9', '8', '7', '6', '5', '4', '3', '2', '1');

     

            if (file_exists("array.txt")) echo "File exists";

            $fh = fopen("array.txt", 'r') or

            die("File Does not exist");

            $line = fgets($fh);

          $numbers[1] = $line;

          $line = fgets($fh);

          $numbers[0] = $line;

          fclose($fh);

          echo "<br />";

          echo $numbers[1];

          echo "<br />";

          echo $numbers[0];

            ?>

     

  3. jcbones

    This script isnt working. Below the script is the out put when the file is run. Line 0 of the array should be 1 sence that is the first character in the .txt file.

    <?php
    $line_of_text = file('ArrayTestFile.txt');
    foreach($line_of_text as $lineNumber => $line) 
        { 
        $line_parts = explode(';',$line); 
        echo 'Line ' . $lineNumber . ' of the array is <br />  ' . implode('<br />  ',$line_parts); 
        }
    echo '<br /><br />You have reached the end of the file!';
    ?>
    

    Line 0 of the array is

      ArrayTestFile.txtArrayTestFile.txtArrayTestFile.t xtArrayTestFile.txtArrayTestFile.txtArrayTestFile .txt

     

    You have reached the end of the file!

     

    MOD EDIT: code tags added.

  4. Then Should I not be able to do this?

     

    <?php   
    $file_handle = fopen("ArrayTestFile.txt", "rb");      
        $line_of_text = fgets($file_handle);   
        $parts = explode(';', $line_of_text);      
        print $parts[0] ." ". $parts[1]." ". $parts[2]." ". $parts[3]." ". $parts[4]." ". $parts[5] ;   
        fclose($file_handle);
        ?>
    

    This is the output of the above code

     

    Notice: Undefined offset: 1 in C:\Program Files (x86)\EasyPHP-5.3.8.0\www\1.php on line 5

    Notice: Undefined offset: 2 in C:\Program Files (x86)\EasyPHP-5.3.8.0\www\1.php on line 5

    Notice: Undefined offset: 3 in C:\Program Files (x86)\EasyPHP-5.3.8.0\www\1.php on line 5

    Notice: Undefined offset: 4 in C:\Program Files (x86)\EasyPHP-5.3.8.0\www\1.php on line 5

    Notice: Undefined offset: 5 in C:\Program Files (x86)\EasyPHP-5.3.8.0\www\1.php on line 5

    ArrayTestFile.txtArrayTestFile.txtArrayTestFile.t xtArrayTestFile.txtArrayTestFile.txtArrayTestFile .txt

     

    MOD EDIT: code tags added.

  5. PLEASE HELP!  :confused:

    I need help I am a new php user, and am trying to understand how to read a .txt file into an array then to retrieve the index of the array at will.

    <?php 
    //echo $myfile = fopen("ArrayTestFile.txt", "r") . "File exists: " or die("File does not exist or you lack permission to open it! ");
    //echo "The contents of the file is " . file_get_contents("ArrayTestFile.txt") . "<br />";
    $fh = fopen("ArrayTestFile.txt", 'r+') or die("Failure");
    $array = array(); //creation of array
    $num = fgets($fh);// recursive call variable $num fgets(.txt line) 
    for ($i = 0; $i <= 5; $i++) //for loop loads first 5 of .txt
       {
        $array["i"] = $num; //loads each line of .txt into the array at the loops index.
        echo "Line " . $i . " of the array is " . $array["i"]; // displays the contents of the array in a print message 
       }
    ?>
    

    Thank you.

     

    MOD EDIT: code tags added.

  6. Hello I am tring to import a .txt file into an array. I am stuck will somone please help me.

    ?<php

    $fp = fopen("ArrayTestFile.txt", 'wb');

    $X = fread("ArrayTestFile.txt" 'w');

    for ($j = 0 ; $j < 6 ; ++$j)

    {

    $written = fwrite($fp, "ArrayTestFile.txt");

    if ($written == FALSE) break;

    }

     

    print    $fp[0] . " " . $fp[1] . " " . $fp[2] . " " . $fp[3] . " " . $fp[4];

    fclose($fp);

    ?>

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.