10

I am new to this but have tried to learn as much as I can before asking questions here. Unfortunately, it is unlikely that I have the vocabulary to ask a clear question. Apologies and thanks in advance.

Is it possible to build an array out of data from several files? Say I had a series of text files and the first line of each file was three tags, separated by commas, that I wanted to be stored in an array of all of the tags from all of the text files, how would I go about that?

For example my file might contain tags, the title of the page and its content:

social movements, handout, international

Haiti and the Politics of Resistance

Haiti, officially the Republic of Haiti, is a Caribbean country. It occupies the western, smaller portion of the island of Hispaniola, in the Greater Antillean archipelago, which it shares with the Dominican Republic. Ayiti (land of high mountains) was the indigenous Taíno or Amerindian name for the island. The country's highest point is Pic la Selle, at 2,680 metres (8,793 ft). The total area of Haiti is 27,750 square kilometres (10,714 sq mi) and its capital is Port-au-Prince. Haitian Creole and French are the official languages.

My desired outcome is a page containing all of the tags used in all of the text files that can each be clicked on to see list of all of the pages containing those tags.

Never mind, for now, that I want to remove duplicate tags. Do I need to read the first line of the first file, explode that line and then write those values to an array? And then do the same with the next file? I have attempted to do this with, firstly:

$content = file('mytextfilename.txt');
//First line: $content[0];
echo $content[0];

that I found here. Followed by stuff about explode that I found here.

$content = explode(",",$content);
print $content[0];

This did not work, probably obviously, but I am in no position to figure out why not. If I have not explained myself well then please ask so that I can attempt to clarify my question.

Thank you for your help, Adam.

5
  • 2
    Can you post some example data from mytextfilename.txt? Commented May 5, 2013 at 23:44
  • 1
    can you also add your expected output Commented May 5, 2013 at 23:44
  • 2
    You might wish to look into str_getcsv() rather than explode. And to read several files, just use glob() and a foreach() to collect the columns. -- You still need to mention if each file only contains one line of content. Otherwise a pretty neat first question. Commented May 5, 2013 at 23:48
  • also if you're reading line by line, why not use file() and a foreach loop? Commented May 5, 2013 at 23:49
  • +1 for really great question Commented May 6, 2013 at 0:09

1 Answer 1

3

You can try:

$tags = array_reduce(glob(__DIR__ . "/*.txt"), function ($a, $b) {
    $b = explode(",", (new SplFileObject($b, "r"))->fgets());
    return array_merge($a, $b);
}, array());

// To Remove Spaces
$tags = array_map("trim", $tags);

// To make it unique
$tags = array_unique($tags);

print_r($tags);

Since you are teething .. you can consider this version

$tags = array(); // Define tags
$files = glob(__DIR__ . "/*.txt"); // load all txt fules in current folder

foreach($files as $v) {
    $f = fopen($v, 'r'); // read file
    $line = fgets($f); // get first line
    $parts = explode(",", $line); // explode the tags
    $tags = array_merge($tags, $parts); // merge parts to tags
    fclose($f); // closr file
}

// To Remove Spaces
$tags = array_map("trim", $tags);

// To make it unique
$tags = array_unique($tags);

print_r($tags);
Sign up to request clarification or add additional context in comments.

7 Comments

Hello, thank you. Would it be possible to break this down to explain what each element means? I am not familiar with any of the language. I have looked up some of the terms but they do not fit together in my head yet (I suppose that you could say that I do not know the grammar). Or, would it be possible to put it into context, I mean, to write it in such a way that I could copy it into a php file, make a couple of text files for it to read and load it on my server to see what the output is? Otherwise I am not sure I can do much with it.
Added simple version you can understand
That is great, thank you. I am having a look through it now, trying to figure it out.
Okay, thank you again for the teething version. Perhaps the output can help me to continue the question. I have three text files in the directory. The tags in File1 are first tag, second tag. The tags in File2 are third tag, second tag. And the tags in File3 are second tag, fourth tag. So you can see there are some duplicate tags. The print_r($tags); outputs: Array ( [0] => first tag [1] => second tag [2] => third tag [5] => fourth tag ). Does this mean that the duplicate tags are being stored in the array but not printed?
No duplicated tag is printed Array ( [0] => first tag [1] => second tag [2] => third tag [5] => fourth tag ). does not look like duplicate to me ... they all have different names
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.