I would like to parse a CSV data into 2d array. In my csv file the headings are as follows, follow by specific values as column.
CaseNumber IncidentDate IncidentTime Location Incident Disposition
10120323 3/21/2009 .... ........ ...... ......
.....
I manage to copy all the CSV file data into an array called, array And in myarray, I have created a 2d-array with only two headers array[0] = (
[0]=> 'IncidentData',
[1]=> 'Disposition')
So my final myarray should be after matching the head in array of CSV and the head in myarray and get the all column values of 'IncidentData' and 'Disposition'
Any ideas?
Here is what I have so far.
<?php
//# Open the File.
if (($handle = fopen("gatech.csv", "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);
}
// take the row'ified data and columnize the array
function columnizeArray($csvarray) {
$array = array();
foreach($csvarray as $key=>$value) {
// reparse into useful array data.
if ($key == 0) {
foreach ($value AS $key2=>$value2) {
$array[$key2] = array();
$array[$key2][] = $value2;
}
}else if ($key > 0){
foreach ($value as $key3=>$value3) {
$array[$key3][] = $value3;
}
}else{
}
}
//print_r($array[1][1]);
return $array;
}
function groupColumns($array = null){
$myarray = array(); //create myarray with sample headers
$myarray = array(array("CaseNumber", "IncidentDate", "IncidentTime", "Location", "Incident", "Disposition" ));
//k = number of columns 0-5 starts k=1 so k-1
foreach($array as $k=>$v){ //make col into row
for($i==0;$i<$k-1; $i++){
print_r($v); //test: prints everything on CSV the columns as row
while($array[0][$i] != $myarray[0][$i])
if($array[0][$k] == $myarray[0][$k])
print_r($myarray[0][$k]);
else
print_r('test : not reading');
}
}
}
$array2 = groupColumns(columnizeArray($csvarray));
?>