[web@test-p ~]# php -B 'echo "机构号\t费用\t(注:-1是减去 1是加上)\n";' -R '$p=explode("|",trim($argn));echo $p[0], "\t", $p[4], "\t", $p[6], "\n";'如果要筛选机构,则:[web@test-p ~]# php -B 'echo "机构号\t费用\t(注:-1是减去 1是加上)\n";' -R '$p=explode("|",trim($argn));if ($p[0] == '620101') {echo $p[0], "\t", $p[4], "\t", $p[6], "\n";}'如果要保存为文件执行,则:$filter = '620101';
$handle = fopen("inputfile.txt", "r");
if ($handle) {
    while (!feof($handle)) {
        $argn = fgets($handle, 4096);
        $p = explode("|", trim($argn));
        if (empty($filter) || $p[0] == $filter) {
            echo $p[0], "\t", $p[4], "\t", $p[6], "\n";
        }
    }
    fclose($handle);
}

解决方案 »

  1.   

    例数据如下
    620102|1001621100592457413|李元珍|77.0|FA|-1.0|井亚莉| 
    620102|1001621100773348426||1080.0|FA|1.0|王明霞| 
    620103|1001621100773707413|勾来庆|224.0|FA|-1.0|梁兴昌          | 
    620103|1001621100773962602||225.0|FA|1.0|张改娣| 
    想做成
    620102  77.0  -1 
    620102  1080  1 
    620103  224  -1 
    620103  225  1 
    合计    1004
    这里有个合计,合计的时候就是根据-1 和1来加减的!!!就这样!!!!
    然后可以单另统计机构的例如只统计620102的
    620102  77.0 -1
    620102   1080 1
    合计      1003
    谢谢大哥大姐们
      

  2.   


    $filter = '620101';
    $handle = fopen("inputfile.txt", "r");
    $total = 0;
    if ($handle) {
        while (!feof($handle)) {
            $argn = fgets($handle, 4096);
            $p = explode("|", trim($argn));
            if (empty($filter) || $p[0] == $filter) {
                $total += $p[6] > 0 ? $p[4] : (0 - $p[4]);
                echo $p[0], "\t", $p[4], "\t", $p[6], "\n";
            }
        }
        fclose($handle);
    }
    echo "合计\t", $total, "\n";
      

  3.   

    楼上是对的,但有些地方是写错啦:
    <?php
    $filter = '620102';
    $handle = fopen("aa.txt", "r");
    $total = 0;
    if ($handle) {

        while (!feof($handle)) {

            $argn = fgets($handle, 4096);        $p = explode("|", trim($argn));

            if (empty($filter) || $p[0] == $filter) {            $total += $p[5] > 0 ? $p[3] : (0 - $p[3]);
                echo $p[0], "\t", $p[3], "\t", $p[5], "<br/>";
            }
        }
        fclose($handle);
    }
    echo "合计\t", $total, "\n";
    ?>执行结果:
    620102 77.0 -1.0
    620102 1080.0 1.0
    合计 1003