My CSV file has this structure
title;firstName;lastName;email;
Sir;Bob;Public;bob.p@gmail.com;
The first row shows the column names. Content starts with the second row. Now I want this to put into an associative array like this
array([0] =>
array(
'title' => 'Sir',
'firstName' => 'Bob',
'lastName' => 'Public',
'email' => 'bob.q@gmail.com'
)
)
What I tried:
$entities = array();
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$num = count($data);
for ($c = 0; $c < $num; $c++) {
$line = array();
//save first row
if ($row == 1) {
foreach (explode(';', $data[$c]) as $field) {
//dont save empty fields
if ($field != "") {
$fields[] = $field;
$line[$field] = array();
}
}
}
//go through contents and save them
foreach ($fields as $field) {
for ($i = 2; $i < count($fields); $i++) {
$cust = explode(";", $data[$c]);
$line[$field][] = $cust[$i];
}
}
$entities[] = $line;
$row++;
}
}
fclose($handle);
}
This gives no error but a strange array that seems to be messed up. Any ideas how I can get my desired structure?