简体   繁体   中英

CSV to TSV (but only 1st and 3rd line) or to a table

I have a CSV file like this:

Name,7680,Value,7684,7685,7688,7689,7690,7697,7698,7699,7700,7701,7702,7703,7704,7710,7711,7712,7733,7737
140101_130002,2,50948,1,21801,316,316,327,500,887,903,900,897,641,654,673,332,310,324,3,6,0,0
140101_133002,2,51046,1,21849,317,316,327,500,887,899,902,896,645,654,678,335,317,325,3,6,2,2
140101_134501,2,51096,1,21874,315,315,326,499,886,899,898,894,638,651,671,335,314,325,3,6,3,3   

The first line are the names and I want to take the first value and the third and make them TSV or at least put them on an html table not all the values.

For example I need this:

Name    Value

140101_130002   50948   
140101_133002   51046

Please help.

this code will do what you want.

    $csv_file = "yourfile.csv"; // Name of your CSV file
    $csvfile = fopen($csv_file, 'r');
    $name_value=array();

    if(is_file($csv_file)){

        fgets($csvfile); // read and ignore the first line
//if you want to show "Name Value" at beginning of the table, just comment this line

        while (!feof($csvfile)){
            $data = fgetcsv($csvfile,4096,",");
            $name_value[]=array($data[0],$data[2]);
        }

        fclose($csvfile);

        echo "<table>";
            foreach($name_value as $data_to_show) {
                echo "<tr><td>".$data_to_show[0]."</td><td>".$data_to_show[1]."</td></tr>";
            }
        echo "</table>";
    }else die('File no good!');

it will echo a table, with:

140101_130002   50948
140101_133002   51046
140101_134501   51096

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM