Jump to content

Retrieving variables from a array that has been loaded with a .txt file


morningside

Recommended Posts

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.

Link to comment
Share on other sites

Example

 

ArrayTestFile.txt

wohlersr; Richie Wohlers; Intermediate; Four Acesmeyersg; Greg Meyers; Novice; Four Aces

 

readArray.php

<?php

$file_handle = fopen("welcome.txt", "rb");

while (!feof($file_handle) ) {

$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);

?>

I hop that I have answerd your query.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

You should be able to do this:

<?php

//get file into an array.
$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); // displays the contents of the array in a print message
}
echo '<br /><br />You have reached the end of the file!';
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

test file

The contents are

1

2

3

4

I need to read the file into an array then access the information latter.

For instance if I wanted to call the "2" again.

echo $myArray[1]; // this needs to call the 2nd spot in the array.

So I think that I need to use a for loop to store each line of the .txt file into an array index.

 

 

[attachment deleted by admin]

Link to comment
Share on other sites

test.txt

1
2
3
4

 

test.php

<?php

//get file into an array.
$line_of_text = file('test.txt');
foreach($line_of_text as $lineNumber => $line) {
  echo '<br />Line ' . $lineNumber . ' of the array is <br />  ' . $line; // displays the contents of the array in a print message
}
echo '<br /><br />You have reached the end of the file!';
?>

 

Output

Line 0 of the array is
  1
Line 1 of the array is
  2
Line 2 of the array is
  3
Line 3 of the array is
  4

You have reached the end of the file!

Link to comment
Share on other sites

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];

        ?>

 

Link to comment
Share on other sites

<?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

  }

  ?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.