/    Sign up×
Community /Pin to ProfileBookmark

multiple flat files to array

I have another problem with flat files. I want to read flat file line by line and post it as array to have easy access to each line. My base code is:

[code=php]
<?
$fp = fopen(“flatfile.txt”, “r”);
$temp = fread ($fp, filesize (“flatfile.txt”));
fclose ($fp);
$datalines = explode (“n”, $temp);
$count = count($datalines);

for ($i=0; $i<count($datalines); $i++) {
echo $i . “: ” . $datalines[$i] . “<br />”;
}
?>
[/code]

It counts all lines and read show them as:

[CODE]0: 1st line
1: 2nd line
2: 3rd line (…)[/CODE]

I want to use it with multiple files. I want to script search in some directory for all “.txt” files and than read each of them exactly the way I showed before. Here’s what did:

[code=php]
<?
$handle = opendir(“dir”);
while (false !== ($file = readdir($handle)))
{
//reads the dir
if ($file != “.” && $file != “..”)
{ //doesn’t show current and previous dir
$ext = substr($file, -4); //gets the file extension
if ($ext == ‘.txt’) //if it is txt
{

$fp = file(“dir/” . $file);

foreach ($fp as $line){
$data = explode(“n”, $line);

for ($i=0; $i<=count($line); $i++) {
echo $i . “: ” . $data[$i] . “<br />”;
}
}

}
}
}
closedir($handle);
?>
[/code]

The output is:

[CODE]0: 1st line of 1st file
1:
0: 2nd line of 1st file
1:
0: 3rd line of 1st file
1:
0: 1st line of 2nd file
1:
0: 2nd line of 2nd file
1:
0: 3rd line of 2nd file
1: (…)[/CODE]

But it’s wrong, it doesn’t count lines in each file properly and show them wrong ?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 14.2009 — Something like this might be a bit easier (untested):
[code=php]
<?php
$dir = "dir";
$files = glob("$dir/*.txt");
foreach($files as $file)
{
echo "<h3>".basename($file)."</h3>n";
$lines = file($file, FILE_IGNORE_NEW_LINES);
foreach($lines as $nbr => $text)
{
echo "$nbr: $text<br />n";
}
}
?>
[/code]
Copy linkTweet thisAlerts:
@NightShift58Apr 15.2009 — NogDog's solution is better.

Your problem was this:
[code=php]
$data = explode("n", $line);
[/code]


By not using the FILE_IGNORE_NEW_LINES flag in your file() call, you were including the "n" that comes with each line. By later exploding() it, you end up with two lines, the actual line of text and a new, empty one.
Copy linkTweet thisAlerts:
@HelleshternauthorApr 23.2009 — Everything working great but I still have some problem with this script. I'm trying to it add actual date - 1 day to each of opened falt files.

Actually output of script looks like:

[B]HEADER of 1st file[/B]

text text text...

[B]HEADER of 2nd file[/B]

text text text...

(...)
[/QUOTE]


I'd like to add date to each, like:


2009/04/24

[B]HEADER of 1st file[/B]

text text text...

2009/04/23

[B]HEADER of 2nd file[/B]

text text text...

(...)
[/QUOTE]


Is this possible? ?
Copy linkTweet thisAlerts:
@NogDogApr 24.2009 — You could use the filemtime() funtion, on each file, then run that result (a unix timestamp integer) through the [url=http://php.net/date]date[/man]() function to get the desired format.
Copy linkTweet thisAlerts:
@HelleshternauthorApr 24.2009 — Is this possible to combine arrays this way?

Like:

$number = array (1, 2, 3)

$letter = array (a, b, c)

$text = array (text1, text2, text3)

And get output:

1

a

text1

2

b

text2

3

c

text3

?
×

Success!

Help @Helleshtern spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...