Reputation: 1273
I have a lot of .txt files which have several lines of content in it. To grab all the txt files in the folder messages, i use this code:
// read all files in messages folder
$dir = 'messages/';
if ($dh = opendir($dir)) {
while(($file = readdir($dh))!== false){
if ($file != "." && $file != "..") { // This line strips out . & ..
$files_list[] = $file;
}
}
}
To get the second line form a txt file, i know i can use this:
$lines = file($file);
$secondline = $lines[1];
But how can i grab all the second lines from all the txt files and put it in a variable, so that i can sort it? Something like. rsort($all_secondlines_allfiles);
Upvotes: 0
Views: 55
Reputation: 760
Put the second line into a global array then use r/sort() function to sort it as needed.
$globalArray = [];
$dir = 'messages/';
if ($dh = opendir($dir)) {
while(($file = readdir($dh))!== false){
if ($file != "." && $file != "..") { // This line strips out . & ..
$lines = file($dir.$file);
$secondline = $lines[1];
$globalArray[] = $secondline;
}
}
sort($globalArray);
print_R($globalArray);
}
Upvotes: 2