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 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算