简体   繁体   中英

jotform csv to associative array

I have a CSV file that contains 26 fields from a http://jotform.com/ data source. The file is delimited by commas and fields are enclosed by double quotes. The data contains commas. This really blows. Also, the CSV is all on one line...

Is anyone familiar with a program that can handle translating the CSV into an associative array?

I would prefer if the array was indexed by the headers instead of numerical keys. I have tried pretty much all of the http://us.php.net/fgetcsv functions with 0 success.

Code I have tried:

<?php
    function get2DArrayFromCsv($file,$delimiter) {
        if (($handle = fopen($file, "r")) !== FALSE) {
            $i = 0;
            while (($lineArray = fgetcsv($handle, 4000, $delimiter)) !== FALSE) {
                for ($j=0; $j<count($lineArray); $j++) {
                    $data2DArray[$i][$j] = $lineArray[$j];
                }
                $i++;
            }
            fclose($handle);
        }
        return $data2DArray;
    }
?>

$file_path = "../../Reunion-Memory-Book-Form.csv";

if (($handle = fopen($file_path, "r")) !== FALSE) {
    # Set the parent multidimensional array key to 0.
    $nn = 0;
    while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
        # Count the total keys in the row.
        $c = count($data);
        # Populate the multidimensional array.
        for ($x=0;$x<$c;$x++)
        {
            $csvarray[$nn][$x] = $data[$x];
        }
        $nn++;
    }
    # Close the File.
    fclose($handle);
}
print'<pre>';print_r($csvarray);print'</pre>';exit;

Since the CSV is obviously a huge mess...I chose to use the Excel dump and here is the working code:

require_once('../../lib/PHPExcel.php');

$file_path = "../../Reunion-Memory-Book-Form.xls";

$inputFileName = $file_path;
$sheetname = 'Responses';

/**  Identify the type of $inputFileName  * */
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
/**  Create a new Reader of the type defined in $inputFileType  * */
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
/**  Advise the Reader that we only want to load cell data  * */
$objReader->setReadDataOnly(true);
/**  Advise the Reader of which WorkSheets we want to load  * */
$objReader->setLoadSheetsOnly($sheetname);
/**  Load $inputFileName to a PHPExcel Object  * */
$objPHPExcel;
try {
    /** Load $inputFileName to a PHPExcel Object  * */
    $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
} catch (Exception $e) {
    die('Error loading file: ' . $e->getMessage());
}

$objWorksheet = $objPHPExcel->getActiveSheet();

$rowdata = $data = array();
$importCells = array(
    'Submission Date', // 0
    'Name', // 1
    'Major', // 2
    'Street Address', // 3
    'Street Address Line 2', // 4
    'City', // 5
    'State / Province', // 6
    'Postal / Zip Code', // 7
    'Country', // 8
    'Home Phone', // 9
    'Cell Phone', // 10
    'Office Phone', // 11
    'E-mail', // 12
    'Employer/Retired', // 13
    'Job Title', // 14
    'Are you planning to attend reunion?', // 15
    'Spouse/Partner Name', // 16
    'Spouse/Partner Employer', // 17
    'Spouse/Partner Job Title', // 18
    'Spouse/Partner University and Class Year', // 19
    'Children: (list names/ages)', // 20
    'CMC Memories', // 21
    'Interests/Hobbies', // 22
    'Student Activities/Clubs', // 23
    'Volunteer Work (include services)', // 24
    'Life Since', // 25
    'Images', // 26
);

$data = array();
foreach ($objWorksheet->getRowIterator() as $row)
{
    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(true);
    $rowdata = array();
    foreach ($cellIterator as $i => $cell)
    {
        if (isset($importCells[$i])) $rowdata[$importCells[$i]] = $cell->getValue();
    }
    $data[] = $rowdata;
}
print'<pre>Array: ';print_r($data);print'</pre>';exit;
exit;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM