Filter on hight of number in textfile

时间:2016-05-17 11:08:24

标签: php

ive been trying this by splitting the lines with substring and than using array unique on it but I cant get it to work properly. the idea is... if the same line is in the file with a lower number... keep the line with the higher number.
textfile:

wood    tiger    22324    Squirrel
john    apple    24574    Squirrel                                  
peter   snuggle  21234    Squirrel                                  
james   coolest  20108    Squirrel                                  
james   coolest  20134    Squirrel

output needed:

wood    tiger    22324    Squirrel  
john    apple    24574    Squirrel                                  
peter   snuggle  21234    Squirrel                                  
james   coolest  20134    Squirrel 

so it basically has to keep the highest numbered item if its the same line (the higher the number the newer the line is).

What I've tried so far:

$file_handle = fopen("file.txt", "rb" , FILE_SKIP_EMPTY_LINES);


while (!feof($file_handle) ) {
    $line_of_text = fgets($file_handle);
    if ($line_of_text[0] === ' ') continue;  
        if ($line_of_text[0] === ' ') continue;  

    $part1 = substr("$line_of_text", ..., ...); 
    $part2 = substr("$line_of_text", ..., ...);
    $part3 = substr("$line_of_text", ..., ...);
    $part1 = explode(' ', $part1);
    $part1 = array_unique($part1);
    $part1 = implode(' ', $part1);      
var_dump ($part1);
}       

file_put_contents('outputfile.txt', implode(PHP_EOL, $lines));

?>                   

1 个答案:

答案 0 :(得分:0)

循环浏览文件时,在关联数组中跟踪“最终”值。在添加数据之前将每行与数组进行比较以避免重复。

<?php

$file_handle = fopen("test.csv", "rb" , FILE_SKIP_EMPTY_LINES);

$finalArray = array();
$numberPartCol = 1;

while (($line_of_text = fgets($file_handle))) {
    if ($line_of_text[0] === ' ') continue;
    // why check the same thing twice?

    // going to assume you have this part working as you didn't ask about it
    $uniqueKeyPart = substr("$line_of_text", ..., ...);
    // remove all the spaces so we can compare the numbers
    $numberPart = str_replace(" ", "", substr("$line_of_text", ..., ...));
    $squirrelPart = substr("$line_of_text", ..., ...);

    if(!array_key_exists($uniqueKeyPart, $finalArray)) {
        $finalArray[$uniqueKeyPart] = array(
            // use preg_replace to get rid of all the extra whitespace
            // if you don't want the spaces at all, just use str_replace as above
            preg_replace("/ +/", " ", $uniqueKeyPart),
            $numberPart,
            preg_replace("/ +/", " ", $squirrelPart)
        );
    }
    else {
        if($finalArray[$uniqueKeyPart][$numberPartCol] < $numberPart) {
            $finalArray[$uniqueKeyPart][$numberPartCol] = $numberPart;
        }
    }
}

// loop through $finalArray to put the data in the format you want for the outputfile.txt
$lines = array();
foreach($finalArray as $row) {
    $lines = implode(",", $row) . PHP_EOL;
}

file_put_contents('outputfile.txt', $lines);

?>

fgetcsv()

请首先考虑更改数据的存储方式。如果你对此没有控制权,我表示同情:(