我有一个csv文件:
aaa null null Null
111 222 333 77
444 123 555 88
------------------
如何判断某一列里面是否有【123】,然后保留此列,删除其他列?
请各位大神指教!谢谢!
------解决方案--------------------
- PHP code
[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); ?>