dtch60248 2012-11-02 04:21
浏览 58

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.

  • 写回答

1条回答 默认 最新

  • douqi1928 2012-11-02 04:36
    关注

    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,
    ));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c