Jump to content

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


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.

Please enclose your code in the "code tags"

 

Also I'm honestly not 100% sure what you are looking for.  If the text file said..

 

1)  Testing

2)  maximum

3)  php

4)  John

 

What type of results are you looking for??  The more examples the better..

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.

I need to read a .txt file into an array. I also need to beable to call on the parts of the arrray later.

I though that the best way to do this was to use a for loop and assign a $varable to each line of the .txt file that is being read.

When posting code, enclose it within the forum's

 . . . 

BBCode tags.

 

If you want each line of the file to be one array element, you can just read it with file and well, there you have it.

 

$array = file('text_file.txt');

Thanks pika.

If I were to do this though $array = file('ArrayTestFile.txt');

How would I then access each piece of the array?

$array[o]; //would this access the first carracter that is read from the file; meaning  1.

 

[attachment deleted by admin]

$array[0] would contain the first line of the file, yes. If you wanted to see the entire structure of the array, and what is in each element you could simply just do this

$array = file('ArrayTestFile.txt');
echo '<pre>';
print_r($array);
echo '</pre>';

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.

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!';
?>

Well, it actually is working to an extent. Beneath the errors, you're seeing the output from the first line of the file, which is in $parts[0]. Notice that the error messages start with the second array index, $parts[1].

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.

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]

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!

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

        ?>

 

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

  }

  ?>

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.