Help with Arrays

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mattiep
Forum Newbie
Posts: 7
Joined: Wed Feb 09, 2005 4:09 am
Location: London, UK

Help with Arrays

Post by mattiep »

Hi,

I am writing a code to take the data from 2 text files and output it to a table. I need to take only one of the columns from the second file only if the value in the first column matches the value in the first file.

e.g.

Code: Select all

data1, data2, data3
123, abc, def
456, ghi, jkl
789, mno, pqr

Code: Select all

data4, data5, data6
987, mno, jkl
654, abc, pqr
321, ghi, def
output all of text file 1, and the data from data6 if data2=data5.

Here is the code that I already have, but I cant get it to work!!

Code: Select all

<?
$fh1 = fopen(&quote;fileone.txt&quote;, 'r');  //the 'r' argument specifies that the file will be opened for read only
$fh2= fopen(&quote;filetwo.txt&quote;, 'r');

//parse the first file
while ($var1ї] = fgetcsv($fh1,1024,&quote;\t&quote;)) //assume this is the lookup file
{
 // bad coding but dont need to do anything
}

//now have an array or arrays which need cleaning up
foreach ($var1 as $key=>$val)
{
    $datafromfileoneї$valї0]]=array(&quote;data2&quote;=>$valї1],&quote;data3&quote;=>$valї2]);
}
unset($var1);
//parse the second file

while ($var2ї] = fgetcsv($fh2,1024,&quote;\t&quote;)) //assume this is the lookup file
{
 // ditto
}

//transform $var2 into an easy lookup 
foreach ($var2 as $key=>$val)
{
     $datafromfiletwoї$valї1]]ї&quote;datafromfileone&quote;] = $valї1];    //this takes the second col and assigns it to the earlier variable with the ssame key as in the second col.
}
unset ($var2); //destroy the variable to free memory

//close the csv files
fclose($fh1);
fclose($fh2);

//assuming this is a standard vlookup with a single input column 
// you can now lookup each item in the first file and find its corresponding value using something like this:
//you could also choose just to read the first file now and perform the lookup on the incoming data
 echo &quote;<table border=\&quote;1\&quote;>&quote;; 
 echo &quote;<tr>&quote;;
 echo &quote;<td>data1</td>&quote;;
 echo &quote;<td>data2</td>&quote;;
 echo &quote;<td>data5</td>&quote;;
 echo&quote;</tr>&quote;;

foreach ($datafromfileone as $key=>$val) 
{
 extract($val);
 echo &quote;<tr>&quote;;
 echo &quote;<td>$key</td>&quote;;
 echo &quote;<td>$data1</td>&quote;;
 echo &quote;<td>$data2</td>&quote;;
 echo &quote;<td>$data5</td>&quote;;
 echo&quote;</tr>&quote;;
}
echo &quote;<table>&quote;;
?>
If you have read this far then thanks!! I know its long, but theres a lot to explain.

TIA
Mattie P


feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Post Reply