使用php匹配csv文件中从行到另一行的数据?

I am newbie in php. Can this be possible to be done?? the script role is to find the match in another row but not on the same column...

the .csv file

**first row**            **second row**         **third row** 

  This is for NNMM        NN/MM@000-45NNMM;1      2001
  AA/BB@000-45AABB;2      -----                   2002
  NN/MM@000-45NNMM;2      -----                   2003
  This is for XXYY        XX/YY@000-45XXYY;1      2004
  LL/QQ@000-45LLQQ;2      -----                   2005
  WW/KK@000-45WWKK;2      -----                   2006
  CC/DD@000-45CCDD;2      -----                   2005
  PP/SS@000-45PPSS;2      -----                   2006
  This is for AABB        AA/BB@000-45AABB;1      2007
  XX/YY@000-45XXYY;2      -----                   2008
  This is for PPSS        PP/SS@000-45PPSS;1      2009
  This is for CCDD        CC/DD@000-45CCDD;1      2010

in first row: there are 5 fields the last char in string is "2".(i.e.AA/BB@000-45AABB;2)

in second row: there are 5 fields the last char in string is "1".(i.e.AA/BB@000-45AABB;1)

now i want to do a matching script where the first row find the match in 2ndrow.. and display the data from first row which the match is found... hope it makes sense...

desired output

  This is for NNMM        NN/MM@000-45NNMM;1      2001
  This is for XXYY        XX/YY@000-45XXYY;1      2004
  This is for AABB        AA/BB@000-45AABB;1      2007
  This is for PPSS        PP/SS@000-45PPSS;1      2009
  This is for CCDD        CC/DD@000-45CCDD;1      2010

i have started a script but stuck working out with the logic...

$file  = fopen('sample.csv', 'r');
echo "<table style='border: 2px solid black; text-align:left'>";
while (($line = fgetcsv($file)) !== FALSE) {  
    list( $row1, $row2, $row3) = $line;

        echo "<tr>";
        echo "<td>$row1</td>"; 
        echo"<td>$row2</td>"; 
        echo "<td>$row3</td>";
        echo "</tr>";       

}
echo "</table>"; 

It is propably not best tool for that, but I tried to do it with regex, and I know it is ugly, but try it out:

(?|(?:([A-Z]{2}\/[A-Z]{2}@\d{3}-\d{2}([A-Z]{4});)(1))(?=(?:.|
)+?(\d{4})(?:.|
)+\1(?:2))|(?:([A-Z]{2}\/[A-Z]{2}@\d{3}-\d{2}([A-Z]{4});)2)(?=(?:.|
)+(?:\1(1))(?:.|n)+?(\d{4})))

DEMO

Relevant data is in groups, so you can use something like: "This is for $2\t$1$3\t$4" to get data you want. In substitution part of my demo, you can see the output (but it only replace matched parts and the the rest of umatched text is still there, but it is just to see how such output could look).

use a do while() instead of while sorry can't write the code for you

and you have to use if statements also if ($row1 == $row1) {then do this}