Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9317317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:50:54+00:00 2026-06-19T02:50:54+00:00

From a csv file I need to extract the header and the values. Both

  • 0

From a csv file I need to extract the header and the values. Both are later accessed in frontend.

$header = array();
$contacts = array();

if ($request->isMethod('POST')) {

   if (($handle = fopen($_FILES['file']['tmp_name'], "r")) !== FALSE) {
        $header = fgetcsv($handle, 1000, ",");

        while (($values = fgetcsv($handle, 1000, ",")) !== FALSE) {

            // array_combine
            // Creates an array by using one array for keys
            // and another for its values
            $contacts[] = array_combine($header, $values);
        }
        fclose($handle);
   }
} 

It works with csv files that look like this

Name,Firstname,Organisation,
Bar,Foo,SO,

I just exported my gmail contacts and tried to read them using the above code but I get following error

Warning: array_combine() [function.array-combine]: Both
parameters should have an equal number of elements

The gmail csv looks like this

Name,Firstname,Organisation
Bar,Foo,SO

Is the last missing , the reason for the error? What is wrong and how to fix it?

I found this on SO

function array_combine2($arr1, $arr2) {
    $count = min(count($arr1), count($arr2));
    return array_combine(array_slice($arr1, 0, $count),
                         array_slice($arr2, 0, $count));
}

This works but it skips the Name field and not all fields are combined. Is this because the gmail csv is not realy valid? Any suggestions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T02:50:56+00:00Added an answer on June 19, 2026 at 2:50 am

    I managed this by expanding the array size or slicing it depending on the size of the header.

    if (count($header) > count($values)) {
        $contacts = array_pad($values, count($header), null);
    } else if (count($header) < count($values)) {
        $contacts = array_slice($values, 0, count($header));
    } else {
        $contacts = $values;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to perform a cURL request to grab a CSV file from the
I need to extract some data from a CSV file. The CSV is a
I need to insert about 1.8 million rows from a CSV file into a
I have a situation where I need to extract dates from the file names
I need to delete some rows from csv file based on a column value.
I have data in a numpy array (read from a .csv file). The relevant
I need to export data from CSV to SQL Server My CSV file is
Using PHP to extract data from SQL and then creating a .csv file on
I need help to generate a tree of categories from a CSV file. I
Possible Duplicate: how to extract data from csv file in php i'm new on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.