日期:2014-05-17 浏览次数:20546 次
[User:root Time:05:47:12 Path:/home/liangdong/php]$ php csv.php [User:root Time:05:47:14 Path:/home/liangdong/php]$ cat csv.out aaa null 111 222 444 123 123 456 [User:root Time:05:47:17 Path:/home/liangdong/php]$ cat csv.php <?php $fin = fopen('csv.txt', 'r'); $fout = fopen('csv.out', 'w'); while (($row = fgetcsv($fin, 0, ' ', ' ', ' ')) !== false) { $index = array_search(123, $row); if ($index !== false) { $filter[$index] = true; } } fseek($fin, 0, SEEK_SET); while (($row = fgetcsv($fin, 0, ' ', ' ', ' ')) !== false) { $out_row = array(); foreach ($row as $index => $value) { if (isset($filter[$index])) { $out_row[] = $value; } } if (!empty($out_row)) { fputcsv($fout, $out_row, ' ', ' '); } } fclose($fin); fclose($fout); ?>