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 6794235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:11:50+00:00 2026-05-26T18:11:50+00:00

I am uploading a csv file and then parsing it using str_getcsv. All works

  • 0

I am uploading a csv file and then parsing it using str_getcsv. All works great except that I need a way to cycle through them. Ideally, it’d be great to have the array come back and look like this:

Array (      
    [1] => Array
       (
            [0] => 1 // first id in csv
            [1] => name
            [2] => address
            [3] => town
            [4] => state
            [5] => zip
            [6] => phone
            [7] => website
            [8] => other
            [9] => other
        )
    [22] => Array
       (
            [10] => name
            [11] => address
            [12] => town
            [13] => state
            [14] => zip
            [15] => phone
            [16] => website
            [17] => other
            [18] => other
        )
    [24] => Array
       (
            [19] => name
            [20] => address
            [21] => town
            [22] => state
            [23] => zip
            [24] => phone
            [25] => website
            [26] => other
            [27] => other
        )
)

However the data comes back like the following:

Array
(
    [0] => 1 // first id in csv
    [1] => name
    [2] => address
    [3] => town
    [4] => state
    [5] => zip
    [6] => phone
    [7] => website
    [8] => other
    [9] => other
22 // id field
    [10] => name
    [11] => address
    [12] => town
    [13] => state
    [14] => zip
    [15] => phone
    [16] => website
    [17] => other
    [18] => other
24// id field
    [19] => name
    [20] => address
    [21] => town
    [22] => state
    [23] => zip
    [24] => phone
    [25] => website
    [26] => other
    [27] => other

Any suggestions on how to fix this to look like the array at the top?
right now I’m doing:

$csvfile = file_get_contents($targetFile);
$csv = str_getcsv($csvfile);
  • 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-05-26T18:11:51+00:00Added an answer on May 26, 2026 at 6:11 pm

    str_getcsv() expects the string passed as parameter to be one record.
    But since your source is a file anyway the easiest way is probably to use fgetcsv() instead of str_getcsv()

    $data = array();
    $fp = fopen($targetFile, 'rb');
    while(!feof($fp)) {
        $data[] = fgetcsv($fp);
    }
    fclose($fp);
    

    self-contained example:

    <?php
    $targetFile = 'soTest.csv';
    setup($targetFile);
    
    $data = array();
    $fp = fopen($targetFile, 'rb');
    while(!feof($fp)) {
        $data[] = fgetcsv($fp);
    }
    var_dump($data);
    
    
    function setup($targetFile) {
        file_put_contents($targetFile, <<< eot
    1,name,address,town,state,zip,phone,website,other,other
    2,name,address,town,state,zip,phone,website,other,other
    3,name,address,town,state,zip,phone,website,other,other
    eot
        );
    }
    

    prints

    array(3) {
      [0]=>
      array(10) {
        [0]=>
        string(1) "1"
        [1]=>
        string(4) "name"
        [2]=>
        string(7) "address"
        [3]=>
        string(4) "town"
        [4]=>
        string(5) "state"
        [5]=>
        string(3) "zip"
        [6]=>
        string(5) "phone"
        [7]=>
        string(7) "website"
        [8]=>
        string(5) "other"
        [9]=>
        string(5) "other"
      }
      [1]=>
      array(10) {
        [0]=>
        string(1) "2"
        [1]=>
        string(4) "name"
        [2]=>
        string(7) "address"
        [3]=>
        string(4) "town"
        [4]=>
        string(5) "state"
        [5]=>
        string(3) "zip"
        [6]=>
        string(5) "phone"
        [7]=>
        string(7) "website"
        [8]=>
        string(5) "other"
        [9]=>
        string(5) "other"
      }
      [2]=>
      array(10) {
        [0]=>
        string(1) "3"
        [1]=>
        string(4) "name"
        [2]=>
        string(7) "address"
        [3]=>
        string(4) "town"
        [4]=>
        string(5) "state"
        [5]=>
        string(3) "zip"
        [6]=>
        string(5) "phone"
        [7]=>
        string(7) "website"
        [8]=>
        string(5) "other"
        [9]=>
        string(5) "other"
      }
    }
    

    edit2: For using the first element of each record as the key in the result array:

    <?php
    $targetFile = 'soTest.csv';
    setup($targetFile);
    
    $data = array();
    $fp = fopen($targetFile, 'rb');
    while(!feof($fp)) {
        $row = fgetcsv($fp);
        $id = array_shift($row);
        $data[$id] = $row;
    }
    var_dump($data);
    
    
    function setup($targetFile) {
        file_put_contents($targetFile, <<< eot
    1,name,address,town,state,zip,phone,website,other,other
    2,name,address,town,state,zip,phone,website,other,other
    3,name,address,town,state,zip,phone,website,other,other
    eot
        );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm exporting an excel file into a CSV and then uploading to a MySQL
we periodically need to import a CSV that looks like this: Name,SpecID,TestResult1,TestResult2,TestResult3 Alex,ASD123,3.23,452.2,232 Craig,DFG444,453.56,345.3,23
I am uploading email addresses from a CSV file, via fgetcsv() , into a
This is kind of a strange one. I have a CSV that I'm uploading
I have the following code for uploading a csv file to a mysql database.
I've been having a problem uploading a CSV file to Heroku and processing it.
I'm uploading a csv/tsv file from a form in GAE, and I try to
When uploading file, is that possible to get the create time of the file
I am a student who needs help in uploading a zip file that contains
When uploading a file (jpeg) via a form in IE7 I am seeing a

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.