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 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集