PHP - 正则表达式 - 数组 - 查找最后一个数字并将它们一起添加

I was given a file that contains something similar to this kind of structure:

12345    ABC  100M 001   2.0  ABC    1010  4510  A01 451  Apple, Johnny A  150

12345    ABC  100M 011   2.0  ABC    1010  4510  A01 451  Apple, Johnny A  80

12345    ABC  100 011   2.0  ABC    1010  4510  A01 451  Apple, Johnny A  80

I need to grab the following sections from this file:

  • Group together the third column (ie. 100M) if they are similar
  • Add together the fourth column (if they are in the same group as the third column)
  • Add up the last column depending on the fourth column

I've managed to do the following:

$List1 = array();
$grab = fopen("file.txt", "r") or die("Can't open file");
$check = fgets($grab);

 while(!feof($grab)) {    
   if (ereg("^[[:digit:]]{5} +ABC +([[:digit:]]{3}[[:alpha:]]?)+ ([[:digit:]]{3})",
     $check, $output)) {
  if (!in_array($output[1], $List1)) {      
    array_push($List1, $output[1]);               
  } 
      if (!in_array($output[2], $List1)) {        
    array_push($List1, $output[2]);                    
  } 
}    
$check = fgets($grab);
 }     
 fclose($grab); 

foreach ($List1 as $list) {
   print "$list <br/>";     
 }

I have managed to somehow group together the third column.
The fourth column is being displayed, but I'm not sure how to group it together into the third column if it's under the same group.
And I'm not sure how to easily grab the last bit in the file/array. Is there a shortcut to getting the last in a file and adding them up?

Thanks in advance for anyone who can help me.

This should do it:

$string = '12345    ABC  100M 001   2.0  ABC    1010  4510  A01 451  Apple, Johnny A  150

12345    ABC  100M 011   2.0  ABC    1010  4510  A01 451  Apple, Johnny A  80

12345    ABC  100 011   2.0  ABC    1010  4510  A01 451  Apple, Johnny A  80';

$third = array();
$fourth = array();

foreach (explode("
", $string) as $line)
{
    // Skip empty lines.
    if (empty($line))
        continue;

    // Clean up any excessive white space.
    $line = trim(preg_replace('~[\s]{2,}~', ' ', $line));

    $info = explode(' ', $line);

    if (!isset($third[$info[2]]))
        $third[$info[2]] = array();

    $third[$info[2]][] = $info;

    if (!isset($fourth[$info[3]]))
        $fourth[$info[3]] = 0;

    $fourth[$info[3]] += (int) end($info);
}

print_r(array(
    'third' => $third,
    'fourth' => $fourth,
));