View Full Version : Working with Arrays

11-09-2007, 06:53 AM
I'm opening up a few text files and insert the contents into a few arrays, and then merging them together - but I'm having difficulty in trying to append some text to the array, here's what I have so far:

$list1 = file('1.txt');
$list2 = file('2.txt');
$list3 = file('3.txt');
$list4 = file('4.txt');

Basically I read the files into the array, then combine them all, and shuffle up the order. But what I want to do is add some text to each so that I can later on figure out which text file each item came from.

What I want is something like this:

$list1 = file('1.txt');
//append some text to say it came from file 1.txt
for (pseudo code){
$list1=$list1 . '###1.txt';

Of course my actual for loop code doesn't work, because it timesout because it takes too much time to go through each item and append the text. I'm hoping there's a php function that allows you to append text without having to go through each item and append the text.

11-09-2007, 07:05 AM
you could use the array_map (http://in.php.net/manual/en/function.array-map.php) function

function appendString($v) {
return $v . '##.txt';
$list1 = file('1.txt');
$list = array_map('appendString', $list1);

11-09-2007, 07:15 AM
n/m..I read the code wrong..great work!

that looks interesting, but won't that overwrite the contents of the array?
Currently my array contains something like this:

and I want to append the textfile name, so that it looks like this:

with the function you gave, I think it would overwrite the values? to just the text file name?