2012-02-24 11 views
0

http://jotform.com/データソースの26個のフィールドを含むCSVファイルがあります。ファイルはコンマで区切られ、フィールドは二重引用符で囲まれています。データにはカンマが含まれています。これは本当に吹く。また、CSVはすべて1行にあります。連想配列にjotform CSVを添付

CSVを連想配列に変換するプログラムに精通している人はいますか?

数値キーの代わりにヘッダーでインデックスが配列されているのが好きです。 http://us.php.net/fgetcsv関数のほとんどすべてを0で成功させようとしました。

コードは、私が試してみました:

<?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; 
+0

更新説明といくつかのコードを追加 –

答えて

0

をCSVは明らかに巨大な混乱ですので...私はダンプExcelを使用することを選択し、ここで作業コードです:

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;