I want to import the data from different CSV file to MySQL. (different files consist required columns on different positions but the header values are same in all the files)
Some CSV files are like;
-------------------------------------------------
| Name, Father Name, Contact, City, email, |
-------------------------------------------------
| Ali, Ahmed, 123456, isb. , ali@mail.com, |
| Fazi, Khan, 123456, hyd. , faiz@ymail.com, |
-------------------------------------------------
And sometime the CSV files are like;
-------------------------------------------------
| Name, Father Name, Contact, email, City, |
-------------------------------------------------
| Ali, Ahmed, 123456, ali@mail.com, isb. , |
| Fazi, Khan, 123456, faiz@ymail.com, hyd. , |
-------------------------------------------------
After exploring and working on many suggestions I got this solution. but I don't know how can I use this code to insert the rows of array in database.
$names = array('Name', 'Father Name', 'contact', 'email');
$picked = array();
$theData = array();
$isFirstRow = true;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$numCols = count($data);
$row = array();
// first row to select columns for extraction
if($isFirstRow) {
for($c=0; $c<$numCols; $c++)
if(!in_array($data[$c], $names)
continue;
else
$picked[] = $c;
$isFirstRow = false;
}
// process remaining rows
else {
for($c=0; $c < $numCols; $c++)
if(in_array($c, $picked))
$row[] = $data[$c];
$theData[] = $row;
}
}
fclose($handle);
}
Kindly help me that how I can insert the data of $theData[] in MySQL according to the $names[]. Thank You in Advance :)