Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructing a PHP array from separate files

Tags:

arrays

php

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.

like image 871
adamburton Avatar asked May 05 '13 23:05

adamburton


1 Answers

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);
like image 78
Baba Avatar answered Sep 29 '22 10:09

Baba