duanlang0025 2013-06-05 16:25
浏览 33

如何在php中查询csv值?

I'm currently using this script:

<?php function jj_readcsv($filename, $header=false) { $handle = fopen($filename, "r"); echo '<table>'; //display header row if true if ($header) {
    $csvcontents = fgetcsv($handle);
    echo '<tr>';
    foreach ($csvcontents as $headercolumn) {
        echo "<th>$headercolumn</th>";
    }
    echo '</tr>'; } // displaying contents while ($csvcontents = fgetcsv($handle)) {
    echo '<tr>';
    foreach ($csvcontents as $column) {
        echo "<td>$column</td>";
    }
    echo '</tr>'; } echo '</table>'; fclose($handle); } jj_readcsv('partitiontable.csv',true);

?>

I have a CSV table with 8 columns. I would like to know if it possible to get only on element of the column when making a query. Here is what look like my CSV :

"HEMSI, Alberto",P_000001,P_1,Partition,169864,"Hemsi, Myriam","HEMSI, Alberto","Una matica de ruda",
"HEMSI, Alberto",P_000002,P_2,Partition,169865,"Hemsi, Myriam","HEMSI, Alberto","Ya salio de la mar",

the idea is : query a value for example: P_1 and get in result: Una matica de ruda

For each line, when asking P_1 getting the value which is in the 8 column. And the same for P_2

  • 写回答

3条回答 默认 最新

  • drfrvbq736383 2013-06-05 16:30
    关注

    Yes, this is possible using two methods:

    1. At the beginning create an associative array and insert as key the 4th column, and as value with 8th column. Then use this as a lookup table in all further code.
    2. If you don't do 1. , you can make a for loop that goes through the CSV data and looks with IF that the 4th row value is the one you are looking for. When found, return the 8th column value.

    Example of first approach with associative array

    <?php
    
    $array = array();
    jj_readcsv("csv.txt");
    
    echo $array["P_1"]."<br/>";
    echo $array["P_2"]."<br/>";
    
    function jj_readcsv($filename, $header = false)
    {
        global $array;
        $row = 1;
        if (($handle = fopen($filename, "r")) !== FALSE) {
        echo '<table>'; //display header row if true if ($header) {
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            //print_r($data);
            $num = count($data);
            echo '<tr>';
            for ($c=0; $c < $num; $c++) {
               if($row > 1)
               echo "<td>$data[$c]</td>";
               else
               echo "<th>$data[$c]</th>";
    
               if($c==2)
               $array[$data[$c]]= $data[7];
            }
            echo '</tr>';
            $row++;
            }
            fclose($handle);
        }
    }
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题