if (move_uploaded_file($uploadData['tmp_name'], $uploadPath)) {
$files = fopen($uploadPath, 'r');
while (($line = fgetcsv($files, 0, ",")) !== FALSE) {
//$line = array_combine($line,$line); try but not working
pr($line);
}
}
I am getting output like that -
Array
(
[Company Name] => Company Name
[Primary First] => Primary First
[Primary Last] => Primary Last
)
Array
(
[0] => Naturfrisk Energy Bar
[1] => Aksel
[2] => Romer
)
Array
(
[0] => The Code Devloper
[1] => vikas
[2] => Tyagi
)
But i need Output like that-
Array
(
[Company Name] => Company Name
[Primary First] => Primary First
[Primary Last] => Primary Last
)
Array
(
[Company Name] => Naturfrisk Energy Bar
[Primary First] => Aksel
[Primary Last] => Romer
)
Array
(
[Company Name] => The Code Devloper
[Primary First] => vikas
[Primary Last] => Tyagi
)
I think you should store first array returned by fgetcsv
for keys for later arrays as the first line would be header line and you want to use that for keys in subsequent arrays being returned.
So something like this would work
if (move_uploaded_file($uploadData['tmp_name'], $uploadPath)) {
$files = fopen($uploadPath, 'r');
$lineNo = 1;
while (($line = fgetcsv($files, 0, ",")) !== FALSE) {
if($lineNo == 1)
$arrHeader = $line;
else
$line = array_combine($arrHeader, $line);
pr($line);
$lineNo++;
}
}
You can use array_combine()
but first you must have headers:
$headers = fgetcsv($files, 0, ",");
while (($line = fgetcsv($files, 0, ",")) !== FALSE) {
$line = array_combine($headers, $line);
print_r($line);
}
And output:
Array
(
[Company Name] => Naturfrisk Energy Bar
[Primary First] => Aksel
[Primary Last] => Romer
)