0
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
)

2 Answers 2

1

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++;
            }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.