【问题标题】:How to create array with CSV file using header in PHP 5.6?如何使用 PHP 5.6 中的标头创建包含 CSV 文件的数组?
【发布时间】:2015-12-16 04:23:18
【问题描述】:


我尝试使用 CSV 文件创建一个数组并使用第一行作为键。在 PHP 中。
我的代码是:

if (($handle = fopen($target_file, "r")) !== FALSE) {
    $size = filesize($target_file);
    if(!$size) {
        echo "Empty file.<br/>";
        exit;
    }
    $csvcontent = array();
    $header = null;
    while ($row = fgetcsv($handle)) {
        if($header === null) {
            $header = $row;
            continue;
        }
        $csvcontent[] = array_combine($header, $row);
    }
    fclose($handle);
}

最终的格式并不是我所需要的。

有人可以帮助我获得更好的结果吗?
如果 CSV 文件是这样的:

id、功能、主机名、域
1,元素6,MTLLXISNET06,本地
2,元素7,MTLLXISNET07,本地
3、爬网管理器、RDICRAWLMANAGER、本地

我需要这样的结果:

Array
(
    [id] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [fonction] => Array
        (
            [0] => Elemental 6
            [1] => Elemental 7
            [2] => Crawl Manager
        )

    [hostname] => Array
        (
            [0] => MTLLXISNET06
            [1] => MTLLXISNET07
            [2] => RDICRAWLMANAGER
        )

    [domain] => Array
        (
            [0] => local
            [1] => local
            [2] => local
        )

)

【问题讨论】:

    标签: php csv


    【解决方案1】:

    只是与 Suyog 的好答案不同的方法:

    if (($handle = fopen($target_file, "r")) !== FALSE) {
        $size = filesize($target_file);
        if(!$size) {
            echo "Empty file.<br/>";
            exit;
        }
    
        $header = false;
        $content = array();
    
        while ($row = fgetcsv($handle)) {
            if (!$header) {
                $header = $row;
                continue;
            }
    
            // $row will be 0=>1, 1=>Elemental 6, 2=>MTLLXISNET06, 3=>local
            // check if content has 0, 1, 2 and 3 keys
            // If not, create them and assign value as an array
            // If those key exist, push new value into the array
            foreach ($row as $key=>$value) {
                if (!array_key_exists($key, $content)) {
                    $content[$key] = array($value);
                } else {
                    $temp = $content[$key];
                    array_push($temp, $value);
                    $content[$key] = $temp;
                }
            }
        }
    
        $final = array_combine($header, $content);
        print_r($final);
    
        fclose($handle);
    }
    

    结果:

    Array
    (
        [id] => Array
            (
                [0] => 1
                [1] => 2
                [2] => 3
            )
    
        [fonction] => Array
            (
                [0] => Elemental 6
                [1] => Elemental 7
                [2] => Crawl Manager
            )
    
        [hostname] => Array
            (
                [0] => MTLLXISNET06
                [1] => MTLLXISNET07
                [2] => RDICRAWLMANAGER
            )
    
        [domain] => Array
            (
                [0] => local
                [1] => local
                [2] => local
            )
    
    )
    

    测试示例:

    $ cat test.txt
    id,fonction,hostname,domain
    1,Elemental 6,MTLLXISNET06,local
    2,Elemental 7,MTLLXISNET07,local
    3,Crawl Manager,RDICRAWLMANAGER,local
    

    测试代码:

    <?php
    $handle = fopen('test.txt', 'r');
    
    $header = false;
    $content = array();
    while ($row = fgetcsv($handle)) {
        if (!$header) {
            $header = $row;
            continue;
        }
    
        foreach ($row as $key=>$value) {
            if (!array_key_exists($key, $content)) {
                $content[$key] = array($value);
            } else {
                $temp = $content[$key];
                array_push($temp, $value);
                $content[$key] = $temp;
            }
        }
    }
    $final = array_combine($header, $content);
    print_r($final);
    ?>
    

    【讨论】:

      【解决方案2】:

      试试下面的代码:

      function csv_to_array($filename='', $delimiter=',')
      {
          if(!file_exists($filename) || !is_readable($filename))
              return FALSE;
      
          $header = NULL;
          $data = array();
          if (($handle = fopen($filename, 'r')) !== FALSE)
          {
              while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
              {
                  if(!$header)
                      $header = $row;
                  else
                      $data[] = array_combine($header, $row);
              }
              fclose($handle);
          }
          return $data;
      }
      

      您可以将此功能用作:

      $tempArray = csv_to_array('myfile.csv');
      

      然后一些简单的循环和操作函数结果如下将给出预期的输出:

      $outArray = array();
      foreach($tempArray as $temp)
          foreach($temp as $key => $val)
              $outArray[$key][] = $val; 
      
      echo "<pre>"; print_r($outArray);
      

      这样做的结果将是:

      Array
      (
          [id] => Array
              (
                  [0] => 1
                  [1] => 2
                  [2] => 3
              )
      
          [fonction] => Array
              (
                  [0] => Elemental 6
                  [1] => Elemental 7
                  [2] => Crawl Manager
              )
      
          [hostname] => Array
              (
                  [0] => MTLLXISNET06
                  [1] => MTLLXISNET07
                  [2] => RDICRAWLMANAGER
              )
      
          [domain] => Array
              (
                  [0] => local
                  [1] => local
                  [2] => local
              )
      
      )
      

      Function source

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-09
        • 1970-01-01
        • 1970-01-01
        • 2014-09-19
        • 2010-11-19
        相关资源
        最近更新 更多