dongyuan2652 2017-04-19 03:25
浏览 23
已采纳

如何在PHP中获取getcsv上的头值

I want to get the value of column header in getcsv but I can't.

Example inputs:

Name , Address , Age
John , California, 26
Michael, L.A , 29

If I am in row2 column2, I want to get the name of header which is Address. But my code returns as L.A.

This is my code:

   <?php
    echo '<table class="tbl">'; 
    while(($data = fgetcsv($handle, 1000, ",")) !== false)
    {  
        $col    = count($data);  
        $header = $data[0]; //I AM SUPPOSED to get this value but this returns to the current cell and not the column header.
        echo '<tr>';
        for ($c=0; $c < $col; $c++) { 
            $cell   = esc_html( $data[$c] );
            $colnum = $c + 1;
            if( $row == 0)
            {    
                echo "<td  style='text-transform:uppercase;'><br><br>". 
                        "<b>{$cell}</b></td>"; 
            }
            else
            { 
                echo "<td>{$cell} = {$header}</td>";  //{$row} = {$colnum} 
            } 
        }
        echo '</tr>'; 
        $row++; 
    }  
    echo '</table>';

Based on the inputs and code above, here is the output:

Name , Address , Age
John = John , California = California , 26 = 26
Michael= Michael , L.A = L.A , 29 = 29

I am supposed to get the header value of each column as shown below:

Name , Address , Age
John = Name , California = Address , 26 = Age
Michael= Name , L.A = Address , 29 = Age

  • 写回答

1条回答 默认 最新

  • drghhp8706 2017-04-19 03:50
    关注

    There might be a better answer, but the code below will work.

    Your CSV data is being returned in the manner below:

    Array ( [0] => Name [1] => Address [2] => Age )  
    Array ( [0] => John [1] => California [2] => 26 ) 
    Array ( [0] => Michael [1] => L.A [2] => 29 )
    

    Updated Answer

    <?php
         $headerValues  = array();
         $counter = 0;
         $row = 0;
         if (($handle = fopen("test.csv", "r")) !== FALSE) {
            echo '<table class="tbl">'; 
            while(($data = fgetcsv($handle, 1000, ",")) !== false)
            {  
                // You need to grab the header values on first iteration
                if ($counter == 0) {
                  // store them in an array
                  $headerValues = $data;
                  // increment counter
                  $counter++;                  
                }                
    
                $col    = count($data); 
                echo '<tr>';
                for ($c=0; $c < $col; $c++) {
                    // grab column name here
                    $headerName = $headerValues[$c]; 
                    $cell   = $data[$c];
                    $colnum = $c + 1;
                    if( $row == 0)
                    {                    
                        echo "<td  style='text-transform:uppercase;'><br><br>". 
                                "<b>{$headerName}</b></td>"; 
                    }
                    else
                    { 
                        echo "<td>{$cell} = {$headerName}</td>";  //{$row} = {$colnum} 
                    } 
                }
                echo '</tr>'; 
                $row++; 
    
            }  
    
            echo '</table>';
        }
        ?>  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵