【问题标题】:How to get the header value on getcsv in PHP如何在 PHP 中获取 getcsv 的标头值
【发布时间】:2017-04-19 03:25:45
【问题描述】:

我想在 getcsv 中获取列标题的值,但我不能。

示例输入:

姓名、地址、年龄
约翰,加利福尼亚,26岁
迈克尔,洛杉矶,29

如果我在第 2 行第 2 列,我想获取标题的名称,即 地址。但我的代码返回为 L.A.

这是我的代码:

   <?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>';

根据上面的输入和代码,输出如下:

姓名、地址、年龄
John = John , California = California , 26 = 26
Michael= Michael , L.A = L.A , 29 = 29

我应该得到每一列的表头值,如下所示:

姓名、地址、年龄
John = 姓名、California = 地址、26 = 年龄
Michael= 姓名、L.A = 地址、29 = 年龄

【问题讨论】:

  • 你想要这些标题Name, Address, Age吗?
  • 这里可能已经回答了。 stackoverflow.com/questions/1853094/…
  • 是的.. 我想每行获取这些标题。因为我想比较 CSV 中的标头和数据库中的标头
  • @norcal 我使用的是 CSV 而不是数据库。它们在获取记录方面不同。在 CSV 中,row1 是标题。
  • 你可以做一件事,在你检查$row==1的代码中,你可以将 header 的值存储在一个变量中。

标签: php csv


【解决方案1】:

可能会有更好的答案,但下面的代码会起作用。

您的 CSV 数据将按以下方式返回:

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

更新答案

<?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>';
    }
    ?>  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-27
    • 2016-10-31
    • 2016-07-25
    • 2015-11-26
    • 2014-11-09
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    相关资源
    最近更新 更多