Skip to content

Instantly share code, notes, and snippets.

@arcticmouse
Forked from jaywilliams/csv_to_array.php
Last active August 29, 2015 14:06
Show Gist options
  • Save arcticmouse/fe7defe3bd4e6d05b159 to your computer and use it in GitHub Desktop.
Save arcticmouse/fe7defe3bd4e6d05b159 to your computer and use it in GitHub Desktop.

Revisions

  1. @jaywilliams jaywilliams revised this gist Apr 30, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion csv_to_array.php
    Original file line number Diff line number Diff line change
    @@ -38,6 +38,6 @@ function csv_to_array($filename='', $delimiter=',')
    * Example
    */

    print_r(csv_to_array('test.csv'));
    print_r(csv_to_array('example.csv'));

    ?>
  2. @jaywilliams jaywilliams revised this gist Apr 30, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion csv_to_array.php
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@
    * @param string $filename Path to the CSV file
    * @param string $delimiter The separator used in the file
    * @return array
    * @link http://gist.github.com/
    * @link http://gist.github.com/385876
    * @author Jay Williams <http://myd3.com/>
    * @copyright Copyright (c) 2010, Jay Williams
    * @license http://www.opensource.org/licenses/mit-license.php MIT License
  3. @jaywilliams jaywilliams created this gist Apr 30, 2010.
    43 changes: 43 additions & 0 deletions csv_to_array.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    <?php
    /**
    * Convert a comma separated file into an associated array.
    * The first row should contain the array keys.
    *
    * Example:
    *
    * @param string $filename Path to the CSV file
    * @param string $delimiter The separator used in the file
    * @return array
    * @link http://gist.github.com/
    * @author Jay Williams <http://myd3.com/>
    * @copyright Copyright (c) 2010, Jay Williams
    * @license http://www.opensource.org/licenses/mit-license.php MIT License
    */
    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;
    }

    /**
    * Example
    */

    print_r(csv_to_array('test.csv'));

    ?>
    3 changes: 3 additions & 0 deletions example.csv
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    "name", "number"
    "Lorem", 11
    "ipsum", 22
    15 changes: 15 additions & 0 deletions result.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    Array
    (
    [0] => Array
    (
    [name] => Lorem
    [number] => 11
    )

    [1] => Array
    (
    [name] => ipsum
    [number] => 22
    )

    )