Created
January 27, 2013 06:36
-
-
Save bkamapantula/4647013 to your computer and use it in GitHub Desktop.
Read CSV file in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function csvToArrayTwo($file, $delimiter) { | |
if (($handle = fopen($file, 'r')) !== FALSE) { | |
$i = 0; | |
while (($lineArray = fgetcsv($handle, 4000, $delimiter, '"')) !== FALSE) { | |
for ($j = 0; $j < count($lineArray); $j++) { | |
$arr[$i][$j] = $lineArray[$j]; | |
} | |
$i++; | |
} | |
fclose($handle); | |
} | |
return $arr; | |
} | |
function processArrayToJSON($type) { | |
if ($type == 0) { | |
$csvdata = 'Everything-gdoc.csv'; | |
} | |
$keysTwo = array(); | |
$newArrayTwo = array(); | |
$dataTwo = csvToArrayTwo($csvdata, ','); | |
$countTwo = count($dataTwo) - 1; | |
$labelsTwo = array_shift($dataTwo); | |
foreach ($labelsTwo as $labelTwo) { | |
$keysTwo[] = $labelTwo; | |
} | |
$keysTwo[] = 'id'; | |
for ($eyeter = 0; $eyeter < $countTwo; $eyeter++) { | |
$dataTwo[$eyeter][] = $eyeter; | |
} | |
for ($k = 0; $k < $countTwo; $k++) { | |
$dTwo = array_combine($keysTwo, $dataTwo[$k]); | |
$newArrayTwo[$k] = $dTwo; | |
} | |
return $newArrayTwo; | |
} | |
$everything = processArrayToJSON(0); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment