Read Text File Into Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
djwk
Forum Commoner
Posts: 56
Joined: Tue Mar 07, 2006 2:14 pm

Read Text File Into Array

Post by djwk »

Hi there

I have a text file named "array.dat" and it contains the following:

7
3
9
10
4
3
1
8
2


How can I load this text file into an array? (Seperated by lines)
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

To put everything into array with new lines :

Code: Select all

<?php

$handle = @fopen("array.dat", "r");
if ($handle) {
   while (!feof($handle)) {
       $buffer .= fgets($handle, 4096);
   }
   $arr[] = $buffer;
   fclose($handle);
}

print_r($arr);
?>
Output :

Code: Select all

Array
(
    [0] => 7
3
9
10
4
3
1
8
2 
)
To put lines into an array :

Code: Select all

<?php

$handle = @fopen("array.dat", "r");
if ($handle) {
   while (!feof($handle)) {
       $buffer = fgets($handle, 4096);
       $arr[] = $buffer;
   }
   fclose($handle);
}

print_r($arr);
?>
Output :

Code: Select all

Array
(
    [0] => 7

    [1] => 3

    [2] => 9

    [3] => 10

    [4] => 4

    [5] => 3

    [6] => 1

    [7] => 8

    [8] => 2 
)
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

Code: Select all

$arr = file('array.dat');
AcidRain
Forum Newbie
Posts: 3
Joined: Sun Jan 21, 2007 5:09 pm

Post by AcidRain »

don't forget to trim array's contents
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

Mordred wrote:

Code: Select all

$arr = file('array.dat');
This will put everything in a variable, not in array.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

dibyendrah wrote:
Mordred wrote:

Code: Select all

$arr = file('array.dat');
This will put everything in a variable, not in array.
Oh, really, you have tried it then? (insert orly owl pic here ;) )


@AcidRain: You are quite right that the new lines will remain in the array elements, here's a quick (but good) fix for that:

Code: Select all

function ArrayTrim(&$value){$value=trim($value);}
$arr = file('array.dat');
array_walk($arr, 'ArrayTrim');
(Using array_walk() is a bit longer, but shoule be better in execution time than using array_map(), because it works in-place and does not copy the array)

Code: Select all

$arr = file('array.dat');
$arr = array_map('trim', $arr);
or even:

Code: Select all

$arr = array_map('trim', file('array.dat'));
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

Mordred wrote:
dibyendrah wrote:
Mordred wrote:

Code: Select all

$arr = file('array.dat');
This will put everything in a variable, not in array.

Oh, really, you have tried it then? (insert orly owl pic here ;) )
Sometimes, brain don't work anymore. yes. $arr becomes array after using file() function.
Post Reply